Monthly Archives: June 2012

Const vs Readonly

 

Const

Readonly

They are Compile-Time constant means always constant. They are Run-time constant.
value can be set at declaration only. value can be set at declaration or in Constructor.
Can’t be static. Can be static

Advertisements

Leave a comment

Filed under C#

Difference between “==” and Equals() methods?

Value Type:- “==” & Equal() method usually compare object by value.

Reference Type:- “==” compares whether both the reference point to same object.
Equal() returns true if value at reference point to the object are equivalent.

there is an exception here for string that “==” compares value rather then object identity.

In the below example:- for value type is obvious, but for reference types s1 & s2 are different objects so it will return false where as both of objects are equivalent so it will return true.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Refrences

http://dotnetguts.blogspot.in/2007/07/difference-between-and-equals-method.html

1 Comment

Filed under .Net, C#

When is a static constructor in C# called?

A static constructor is invoked by the first of either of the following conditions:

  • Create an instance of the class.
  • Refer any of the static methods of the class.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

For Generic Class, using different type arguments you’ll get different concrete classes, so Static Constructor will be called multiple time (once for each type)

Leave a comment

Filed under C#

Polymorphism

Polymorphism means same operation can be defined in multiple forms.
There are two types of polymorphism:
Compile Time & Run-time polymorphism

Example of Compile Time polymorphism:- Method Overloading

It means having two or more methods having same name but different signature.
-the methods can be in the same class or different class (inherited).
-constructor overloading is also possible.
-it is not mandatory to have different return type, but either the number of parameters or parameters type should be different.

Example of Run time Polymorphism:- Method Overriding

Two or more methods with same signature but different implementation but in different class (base & inherited).

eg:- Using New & Override

1 Comment

Filed under C#

Why do we need Constructor for abstract class?

Yes we can have constructor in Abstract class (though we can’t still create object of abstract class).
Because :- this way you can instantiate the data in abstract class.
And inherited classes can call the base constructor.

public abstract class A{
private string data;
protected A(string myString){
data = myString;
}
}

public class B : A {

B(string myString) : base(mystring){}
}

NOTE:- As you can’t override constructor, so we can’t have abstract constructor.

Leave a comment

Filed under .Net, C#