Wednesday, July 18, 2007

Facts About Interfaces

1. Interface can'nt have member fields(variables).
2. It can have only method, property, event and indexer as its member.
3. Can'nt use access modifier with members.
4. All members are public abstract by default so can'nt declare them public abstract explicitly.
5. In the implementation class all the member of the interface should be implemented and they can have only public access modifier.
6. If implementation class does not implement any one of the members of the interface then that member and the implementation class both should be declared abstract.

Facts About Abstract Classes

1. Abstract class can'nt have abstract constructor because constructor can'nt be inherited.
2. It can have destructor but it can'nt be abstract or virtual.
3. Abstract class can'nt be a sealed class.
4. Member fields can'nt be abstract.
5. It can have abstract property and events.
6. It can have static and virtual events.
7. Abstract and virtual property, event or method can'nt be private although it can be protected,internal or protected internal.
8. Abstract members can'nt have virtual modifier because they are implicitly virtual.
9. Can'nt change access modifier while overriding member of the abstract class in the derived abstract or contrete class.
10. Any class derived from abstract class either should define all the abstract method of the abstract base class or should be declared abstract class as well. In that case derived abstract class needs not to declare abstract member of base class again.
11. It is possible to have abstract class without any abstract members (method, event or property etc.). But a non abstract class can not have any abstract member.
12. Abstract class can be derived from concrete (non abstract) class.

Facts About Static Classes

1. Static class can have only static member, no instance member is allowed.
2. It can have only static parameterless constructor without access modifier so no constructor overloading is allowed.
3. Can'nt create object of the static class.
4. Static class is a sealed class by default so you can'nt drive class from the static class.
5. Any derived class can'nt be declared as static so static classes can'nt participate in the inheritance.
6. Static class can'nt have destructor.
7. It can have static events.
8. Static members can'nt be marked as abstract, virtual or override.
9. Make the static member public to access them using class name otherwise in case of private, protected or internal access modifier, they can'nt be accessed.
10. Static member of the abstract class can be accessed using the name of the derived class.

Friday, July 13, 2007

C# struct/class Differences

1. Classes are reference types and structs are value types so a class variable can be assigned null but we cannot assign null to a struct variable.
2. When you instantiate a class, it will be allocated on the heap.When you instantiate a struct, it gets created on the stack.
3. You will always be dealing with reference to an object ( instance ) of a class. But you will not be dealing with references to an instance of a struct ( but dealing directly with them ).
4. When passing a class to a method, it is passed by reference. When passing a struct to a method, it's passed by value instead of as a reference.
5. You cannot have instance Field initializers in structs.But classes can have initializers.
6. Classes can have explicit parameterless constructors. But structs cannot have explicit parameterless constructors.
7. Classes must be instantiated using the new operator. But structs can be instantiated without using the new operator.
8. Classes support inheritance.But there is no inheritance for structs. ( structs don't support inheritance polymorphism ) So we cannot have a base structure and a derived structure. But like classes, structures can implement interfaces.
9. Since struct does not support inheritance, access modifier of a member of a struct cannot be protected or protected internal.
10. It is not mandatory to initialize all Fields inside the constructor of a class.
But all the Fields of a struct must be fully initialized inside the constructor.
11. A class is permitted to declare a destructor.But a struct is not permitted to declare a destructor.
12. classes are used for complex and large set data. structs are simple to use. structs are useful whenever you need a type that will be used often and is mostly just a piece of data.
13. Creating a struct instance cannot cause a garbage collection (unless the constructor directly or indirectly creates a reference type instance) whereas creating a reference type instance can cause garbage collection.
14. A struct always has a built-in public default constructor. This means that a struct is always instantiable whereas a class might not be since all its constructors could be private.
15. A struct is implicitly sealed, a class isn't.
16. A struct can't be abstract, a class can.
17. A struct can't call : base() in its constructor whereas a class with no explicit base class can.
18. A struct can't declare abstract function members, an abstract class can.
19. A struct can't declare virtual function members, a class can.
20. A struct can't declare sealed function members, a class can.
21. A struct can't declare override function members, a class can. The one exception to this rule is that a struct can override the virtual methods of System.Object, viz, Equals(), and GetHashCode(), and ToString().

Wednesday, July 11, 2007

Web Garden Model

The Web garden model is configurable through the section of the machine.config file. Notice that the section is the only configuration section that cannot be placed in an application-specific web.config file. This means that the Web garden mode applies to all applications running on the machine.
Two attributes in the section affect the Web garden model. They are webGarden and cpuMask. The webGarden attribute takes a Boolean value that indicates whether or not multiple worker processes (one per each affinitized CPU) have to be used. The attribute is set to false by default. The cpuMask attribute has default value 1.
Web gardening enables multiple worker processes to run at the same time. However, you should note that all processes will have their own copy of application state, in-process session state, ASP.NET cache, static data, and all that is needed to run applications. When the Web garden mode is enabled, the ASP.NET ISAPI launches as many worker processes as there are CPUs, each a full clone of the next (and each affinitized with the corresponding CPU). To balance the workload, incoming requests are partitioned among running processes in a round-robin manner. Worker processes get recycled as in the single processor case.
When you enable a web-garden in IIS6.0, It creates multiple w3wp.exe to service that application pool. Each w3wp.exe process has it's own memory, threads etc.

Web gardening has some side effects that you should be aware of:

1. If your application uses session state, it must choose an out-of-process provider (NT Service or SQL).
2. Application state and application statics are per process, not per computer.
3. Caching is per process, not per computer.


Thursday, July 5, 2007

Attributes In C#

C# provides a mechanism for defining declarative tags, called attributes, which you can place on certain entities in your source code to specify additional information. The information that attributes contain can be retrieved at run time through reflection. You can use predefined attributes or you can define your own custom attributes.

Every custom attribute class needs 2 things.

1. An AttributeUsage Attribute to declare the class as an attribute. It specifies the language elements to which the attribute can be applied.
2. This class must be derived directly or indirectly from System.Attribute class.

Here is an Example:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method,AllowMultiple=true)]
public class MyAttribute : Attribute
public double _version;
string _name;
public MyAttribute(string name,double ver)
this._name = name;
this._version = ver;

//This method will retrive the attribute information.
public static void GetMyAttributeInfo(Type t)
object[] ClsAttrib =t.GetCustomAttributes(false);
MyAttribute MyAt=(MyAttribute)ClsAttrib[0];
System.Windows.Forms.MessageBox.Show("Name- " + MyAt._name + " , Version" + MyAt._version);
MethodInfo[] TheMethods = t.GetMethods();
for (int i = 0; i < TheMethods.GetLength(0); i++)
MethodInfo MethInfo = TheMethods[i];
object[] Attribs = MethInfo.GetCustomAttributes(false);
foreach (Attribute Attrib in Attribs)
if (Attrib is MyAttribute)
MyAttribute MyAtt = (MyAttribute)Attrib;
System.Windows.Forms.MessageBox.Show("Name- " + MyAtt._name + " , Version" + MyAtt._version);


public class TestMyAttribute
[MyAttribute("ajay", 1.0)]
public void show()

public static void Main()
Base.TestMyAttribute At = new TestMyAttribute();


Creating Assembly Dynamically

public void CreateAssembly()
AppDomain AppDom = AppDomain.CurrentDomain;

// create a new dynamic assembly
AssemblyName AsmName= new AssemblyName();
AsmName.Name = "MyAssembly";
AssemblyBuilder AsmBuild = AppDom.DefineDynamicAssembly(
AsmName, AssemblyBuilderAccess.Run);

// create a new module to hold code in the assembly
ModuleBuilder ModBuild = AsmBuild.DefineDynamicModule("MyModule");

// create a type in the module
TypeBuilder TypeBuild = ModBuild.DefineType(
"MyClass", TypeAttributes.Public);

// create a method of the type
Type ReturnType = typeof(int);
Type[] ParamsType = new Type[0];
MethodBuilder MethBuild = TypeBuild.DefineMethod("MyMethod",
MethodAttributes.Public, ReturnType, ParamsType);

// generate the MSIL
ILGenerator Gen = MethBuild.GetILGenerator();
Gen.Emit(OpCodes.Ldc_I4, 1);


// Now consume the type that is created above

Type TypeObj = TypeBuild.CreateType();

// create an instance of the new type
Object Obj = Activator.CreateInstance(TypeObj);
// create an empty arguments array
Object[] ObjArr = new Object[0];
// get the method and invoke it
MethodInfo MethInfo = TypeObj.GetMethod("MyMethod");
int RetVal = (int)MethInfo.Invoke(Obj, ObjArr);
MessageBox.Show("Method " + MethInfo + " in Class " + TypeObj + " returned " + RetVal);



Application Domains in C#

Historically, process boundaries have been used to isolate applications running on the same computer. Each application is loaded into a separate process, which isolates the application from other applications running on the same computer.

The applications are isolated because memory addresses are process-relative; a memory pointer passed from one process to another cannot be used in any meaningful way in the target process. In addition, you cannot make direct calls between two processes. Instead, you must use proxies, which provide a level of indirection.

Application domains provide a more secure and versatile unit of processing that the common language runtime can use to provide isolation between applications. You can run several application domains in a single process with the same level of isolation that would exist in separate processes, but without incurring the additional overhead of making cross-process calls or switching between processes. The ability to run multiple applications within a single process dramatically increases server scalability.

Application domains has the following benefits:

1. Faults in one application cannot affect other applications.
2. Individual applications can be stopped without stopping the entire process.
3. Code running in one application cannot directly access code or resources from another application.Objects that pass between domains are either copied or accessed by proxy.
4. Permissions granted to code can be controlled by the application domain in which the code is running.

To access the code from another application, you can either load the assembly into the current application domain or create a new application domain and load the assembly into it.

Accessing Assembly Code in Current Application Domain

Suppose you have an assembly named MyNameSpace.exe in the c:\ that you want to access in current application domain.

C# Copy Code
static void Main()
// Load the assembly into the current appdomain:
System.Reflection.Assembly newAssembly = System.Reflection.Assembly.LoadFrom(@"c:\MyNameSpace.exe");

// Instantiate RemoteObject:

Accessing Assembly Code in Another Application Domain

When loading the assembly into a separate application domain, use AppDomain.ExecuteAssembly to access the default entry point or AppDomain.CreateInstance to create an instance of the class.

C# Copy Code
static void Main()
System.AppDomain NewAppDomain = System.AppDomain.CreateDomain("NewApplicationDomain");

// Load the assembly and call the default entry point:

// Create an instance of RemoteObject:
NewAppDomain.CreateInstanceFrom(@"c:\MyNameSpace.exe", "MyNameSpace.MyClass");