1.You cannot declare fields,abstract,static as virtual.
2.You can override Methods,Properties(get;set;),indexers.
public virtual String message = "sdad"; ---Error Field Cannot be Virtual.
1.You cannot declare fields,abstract,static as virtual.
2.You can override Methods,Properties(get;set;),indexers.
public virtual String message = "sdad"; ---Error Field Cannot be Virtual.
constants (const): The value of const is set at compile time(the value must be set when you declare it) and cannot be changed at run time.
ReadOnly : They need not be initialized when declared ,but must be initialized in constructor. They could be initialized else where other than constructor.
public static const int s =1 --Error cannot be static
2 .const can be used only with primitive types(exception is Strings) .i.e it does not make sense to use custom class with const.
public cosnt myclass = new myclass() ---Error must be a compile time value
.
Its possible for you to do that with ReadOnly.
Object that has to be Copied:
Shallow Copy: Reference to objects being copied are only modified in shallow copy.
Deep Copy: A separate copy of object is made,instead of only reference.
When you make an Assumption of function and it it violates the assumption ,then you might probably need to throw an exception. For instance,Let’s assume that we have function which returns true if the length of List is greater than 50 and false if it is less than 50.Now what if the list is Null?(function Doe not have assumption ) ,this creates the necessary to throw exception
//This functions assume that you past list item filled with inetgers. //It is not assumed that list might be null,so here you need to throw exception private static bool ValidateListLength(List<int>l) { try { return l.Count>50; } catch { throw ; } return false; }
You cannot Invoke Explicit Interface with Class Instance,You require Interface Instance to call the Explicit Interface
class Sample:ITest { //Interface Defined Explicitly void ITest.Test() { Console.WriteLine(&quot;Hello&quot;); } }
To Invoke the explicit interface,you need Interface Instance,not the class instance
/*You cannot invoke Explicit Interface with Class Instance but the Interface instance. ITest is the inetrface instance */ ITest s1 = new Sample(); s1.Test();
Let’s Assume we have two interface with same contract:
interface ITest { void Test(); //Same Contract as of other interface } interface ITest1 { void Test(); //Same Contract as of other interface }
To define both the Interface with same contract in a class,you have to define Interface Explicitly without Access Modifier.
class Sample:ITest,ITest1 { void ITest.Test() { Console.WriteLine("Hello"); } void ITest1.Test() { Console.WriteLine("Hello1"); } }
To invoke the specific Contract,
//You cannot invoke Explicit Interface with Class Instance. ITest s1 = new Sample(); s1.Test(); ITest1 s2 = new Sample(); s2.Test();
you can throw exception in two ways.
1.throw. This will throw the caught exception.The throwed exception must be caught again.It will not reset the stack trace.
2.throw e or throw new CustomException : It behaves exactly same but it will reset the stack trace.
Experimentation of Throw vs Throw ex(throw new CustomException):
DivByZero Method:
private static void DivByZero() { int j = 0; var result = 9/j; //Line No :45 in my program is the Cause of Exception }
1.Throw:It does not reset the stack trace,instead preserves it and throws the exception
private static void thrwonigException(){ try { DivByZero(); }catch (DivideByZeroException e) { /*It will not reset stack trace,instead throws the caught exception */. throw ; } }
It still maintains the line which causes the exception,
2.Throw e : Returns the line no 34 as cause of exception but not 45,the stack trace is reset after this line
private static void thrwonigException(){ try { DivByZero(); } catch (DivideByZeroException e) { /*It will reset the stack trace and tells that this line is the cause of exception. * It will not who caused the exception. * */ throw e; //or throw new DivideByZeroException(); } }
C# Provides try,catch and finally mechanism to handle Exception.
try: Business logic code block,potentially might cause exception
catch: Block where all your exceptions are caught.
Finally:Block which always executes after try/catch irrespective of exceptions.( P.S: Stackoverflow will make the finally block not to execute)
Rules:
1.There must be finally block after try,If you do not have a catch block.
2.There can be two or more catch blocks.
3.Ordering of Catching Exception Matters.
4.Exception is the base of all other ExceptionClasses.
try { /*your code here*/ } catch(Exception exception) { /* Exeption caught if something goes wrong*/ } finally { /*Guaranteed to run even if exception occurs.This block will always run*/ Console.WriteLine(); /* * Clean UP Code. */ }
AppDomain is a mechanism where the application executes in Isolated Environment.
By Default,there is one AppDomain per process. A process may contain one or more AppDomain.
Any Exception or violation in One AppDomain will not affect other AppDomain or the process.
They are generally used when you load assembly to your current application.The loaded Assembly can be executed in separate AppDomain.
To Deploy Assembly into GAC,It must be strongly named Assembly.
To Add assembly,you can use GACUTIL.exe.A utility Microsoft ships to manage GAC.
To install :
gacutil.exe -i yourapp.dll
To Remove:
gacutil.exe -u yourapp.dll
You can locate the installed assembly at (C:\\Windows\Microsoft.NET\GAC_UTIL)