#Things you may want to know about Constants and Readonly

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.

  1. const are themselves static, so you cannot declare
     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.

 

#55: When should you throw exception ?

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;
 }

#54: Invoking Explicit Interface-Cannot Invoke with Class Instance,but requires Interface Instance

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("Hello");
 }
 }

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();

#53 Mutiple Interface with Same Contract

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();

#47: throwing Exception

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,

throw

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();
 }
}

Throw Ex