Monday, July 16, 2007

Basics

Value and Refference types
By default in C# everything is passed as value type.

Structs are value type and Classes are reference type. Value types are assigned memory in stack with an exception of In-Line value types asssigned in heap. Reference types are always assigned memory in heap.

value type means a copy of whatever is being passed either a reference type or a value type.



Interface - Abstract Class
What is an Interface?

An interface is a contract, a specification that concrete classes MUST follow. It defines method signatures but cannot have any implementations; the latter must be provided by the classes that implement the interface.
For example,
public interface Vehicles
{
string Engine
{
get;
set;
}
void Accelerator();
}

what is an abstract class?

An Abstract class lets you define some behaviors and force your subclasses to provide others.
For example,
public abstract class Vehicles
{
private int noOfWheel;
private string color;
public abstract string Engine
{
get;
set;
}
public abstract void Accelerator();
}


Differences between Interfaces and Abstract classes ?

I. multiple inheritance

A class may implement several interfaces but can only extend one abstract class.

II. default implementation

An interface cannot provide any code at all, much less default code. An abstract class can provide complete code, default code, and/or just stubs that have to be overridden.

III. adding functionality

If you add a new method to an interface, you must track down all implementations of that interface in the universe and provide them with a concrete implementation of that method.
If you add a new method to an abstract class, you have the option of providing a default implementation of it. Then all existing code will continue to work without change.

IV. is-a vs -able or can-do

Interfaces are often used to describe the abilities of a class, not its central identity, e.g. an Automobile class might implement the Recyclable interface, which could apply to many otherwise totally unrelated objects.

An abstract class defines the core identity of its descendants.


Abstract class can contain abstract methods, abstract property as well as other members (just like normal class).

Interface can only contain abstract methods, properties but we don’t need to put abstract and public keyword. All the methods and properties defined in Interface are by default public and abstract.


With abstract classes we can provide a default/general method implementation for all overridding classes whereas in case of interfaces, every implementing class must provide its own specific implementation.




Static (Type) or Instance Constructer
Static or Type Constructor :

used to provide a one way access to the class object. you can restrict the class intance to itself using a static constructer. it also provide ways to make singelton class or a clone function, which always returns the class object.
Only one class constructor per type is permitted.
It cannot use the vararg (variable argument) calling convention.

Instance Constructor :

is executed every time a new object is created and is specific to that object instance.
Multiple instance construtor are allowed with different parameter list (return type ie void and name are fixed).
It cannot use the vararg (variable argument) calling convention.
Instance constructors are not allowed to be a virtual type.




Ex :

using System;
class SomeClass {

private Int32 member1;
private static Int32 member2;

//Type constructor
static SomeClass(){}

//Instance constructor
public SomeClass(){} //default instance Constructor
public SomeClass(Int32 memberval){ member1 = memberval;}


}



Working Ex:

public class A

{

//Static Constructor of Class A

static A()

{

Console.WriteLine("Static Constructor of Class A");

}

//Constructor of Class A

public A()

{

Console.WriteLine("Constructor of Class A");

}

}

public class B: A

{

//Static Constructor of Class B

static B()

{

Console.WriteLine("Static Constructor of Class B");

}

//Constructor of Class B

public B()

{

Console.WriteLine("Constructor of Class B");

}

}

public class C: B

{

//Static Constructor of Class A

static C()

{

Console.WriteLine("Static Constructor of Class C");

}

//Constructor of Class C

public C()

{

Console.WriteLine("Constructor of Class C");

}

}

#endregion

class ConstructerDestructer

{

static void Main(string[] args)

{

C c = new C();

Console.ReadKey();

}

}



Output :

Static Constructor of Class C

Static Constructor of Class B

Static Constructor of Class A

Constructor of Class A

Constructor of Class B

Constructor of Class C





Interview

ref and out?
A ref parameter must be assigned a value by the CALLER before it is sent to the callee. An out parameter, on the other hand, is sent unassigned by the caller and must be assigned by the CALLEE


why cant i use static and const together?
All constants declarations are implicitly static, and the C# specification states that the (redundant) inclusion of the static modifier is prohibited.


Difference b/w const(compile time constant) and static readonly(set at run time, containing class is allowed to modify) ?


Can we const array of class/struct ?

const int constIntArray = 2; -valid-
int [] xconstIntArray = new int [] {2, 3, 4}; -valid-
const int [] xconstIntArray = new int [] {2, 3, 4}; -not valid- 'const can only be applied to a field or local whose value is known at compile time.'


What is a delegate? Comparision with function pointers?

A delegate is a data structure that refers to a static method or to an object instance and an instance method of that object.
function pointer can only reference static functions, a delegate can reference both static and instance methods.( In the latter case, the delegate stores not only a reference to the method’s entry point, but also a reference to the object instance for which to invoke the method.)


AppDomain & WebServices?

Assembly(physical) & Namespaces(logical)?

Assembly(contains verious modules) & Module(smallest executable part in a assembly)?

Can i generate and load a Assembly at runtime?
yes, use System.Reflection.Emit



Comparision b/w Equals and ReferenceEquals?

if ReferenceEquals true, then Equals true; but vice versa is always not true. ReferenceEquals compare the object pointed to whereas Equals(String) compare value. In case of object type Equals comparision, it compares the object pointed to.

ex:

public static void ReferenceValue(object a1, object a2){

if(object.ReferenceEquals(a1,a2))

Console.WriteLine("Reference Equals");

else

Console.WriteLine("Reference Not Equals");

if(object.Equals(a1,a2))

Console.WriteLine("Value Equals");

else

Console.WriteLine("Value Not Equals");}



string a = "luck"; string b = "luck"; string c = "l"; int s = 1;

Program.ReferenceValue(a, b); // 'Reference Equals' , 'Value Equals'

Program.ReferenceValue(a, c); // 'Reference Not Equals' , 'Value Not Equals'

Program.ReferenceValue(a, s); // 'Reference Not Equals' , 'Value Not Equals'

a = Console.ReadLine(); // enter 'luck'

Program.ReferenceValue(a, b); // 'Reference Not Equals' , 'Value Equals'

Console.ReadKey();

Console.Clear();

a = Console.ReadLine(); //enter 'luck'

b = Console.ReadLine(); //enter 'luck'

Program.ReferenceValue(a, b); // 'Reference Not Equals' , 'Value Equals'

Console.ReadKey();



Virtual Function?

Ex:

using System;
namespace SomeCorpProject
{
public class BaseBusinessObject
{
public BaseBusinessObject() {
}
public override string ToString() {
return "BaseBusinessObject";
}
}
}

using System;
namespace SomeCorpProject
{
public class NCapacitor : BaseBusinessObject
{
public NCapacitor()
{
}
}

public class Capacitor : BaseBusinessObject
{
public Capacitor() {
}
public override string ToString() {
return "Capacitor";
}
}
}

using System;
namespace SomeCorpProject
{
class Class1 {
[STAThread]
static void Main(string[] args) {
BaseBusinessObject bizObj = new Capacitor();
System.Console.WriteLine(bizObj.ToString());
BaseBusinessObject NbizObj = new NCapacitor();
System.Console.WriteLine(NbizObj.ToString());
}
}
} What will happen when this code is compiled and run?

(BaseBusinessObject overrides object.ToString)
---------The word "Capacitor" will be written to the console.(overrides BaseBusinessObject's object.ToString)
---------The word "BaseBusinessObject" will be written to the console.(doesnt overrides BaseBusinessObject's object.ToString)

No comments: