Thursday, 23 January 2014

What's the difference between struct and class in C# .Net ?

In .NET, there are two categories of types, reference types and value types.

Structs are value types and classes are reference types.
The general difference is that a reference type lives on the heap, and a value type lives inline, that is, wherever it is your variable or field is defined.
A variable containing a value type contains the entire value type value. For a struct, that means that the variable contains the entire struct, with all its fields.
A variable containing a reference type contains a pointer, or a reference to somewhere else in memory where the actual value resides.
This has one benefit, to begin with:
*value type*s always contains a value
reference types can contain a null-reference, meaning that they don't refer to anything at all at the moment
Internally, *reference type*s are implemented as pointers, and knowing that, and knowing how variable assignment works, there are other behavioral patterns:
copying the contents of a value type variable into another variable, copies the entire contents into the new variable, making the two distinct. In other words, after the copy, changes to one won't affect the other
copying the contents of a reference type variable into another variable, copies the reference, which means you now have two references to the same somewhere else storage of the actual data. In other words, after the copy, changing the data in one reference will appear to affect the other as well, but only because you're really just looking at the same data both places
When you declare variables or fields, here's how the two types differ:

variable: value type lives on the stack, reference type lives on the stack as a pointer to somewhere in heap memory where the actual memory lives
class/struct-field: value type lives inside the class, reference type lives inside the class as a pointer to somewhere in heap memory where the actual memory lives.
Classes can be nothing - the reference can point to a null.
Structs are the actual value - they can be empty but never null. For this reason structs always have a default constructor with no parameters - they need a 'starting value.


For Example:

struct MyStruct 
{
    string MyProperty { get; set; }
}

void ChangeMyStruct(MyStruct input) 
   input.MyProperty = "new value";
}

...

// Create value type
MyStruct testStruct = new MyStruct { MyProperty = "initial value" }; 

ChangeMyStruct(testStruct);

// Value of testStruct.MyProperty is still "initial value"
//  the method changed a new copy of the structure.



*********For a class this would be different*******

class MyClass 
{
    string MyProperty { get; set; }
}

void ChangeMyClass(MyClass input) 
   input.MyProperty = "new value";
}

...

// Create reference type
MyClass testClass = new MyClass { MyProperty = "initial value" };

ChangeMyClass(testClass);

// Value of testClass.MyProperty is now "new value" 
// - the method changed the instance passed.

Tuesday, 14 January 2014

What is Static Class ?


  • Static Class is same as of non Static class, the only difference i.e. its can not be instantiated means you can not create new instance of the static class.
  • In other words you cannot use NEW keyword to create a variable of class type.
  • For static class ,static constructor is called only once before the class is referenced for the first time in a program loaded once is life time of program and a static class remains in memory for the lifetime of the application domain in which your program resides. 
  • You can directly access the members of a static class by using the class name itself.
          For Example:
          If you have a class with name ClassA and it has public method name MethodA 
          ,then we can access method of class as shown below.
                         
                                         ClassA.MethodA();
      


  •  A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields  
                For example:
    • In the .NET Framework Class Library, the static System.Math class contains       methods that perform mathematical operations, without any requirement to store or retrieve data that is unique to a particular instance of the Math class. That is, you apply the members of the class by specifying the class name and the method  name, as shown in the following example.
                          double dub = -3.14;
                         Console.WriteLine(Math.Abs(dub));



     The following list provides the main features of a static class:
  • Contains only static members.
  • Cannot be instantiated.
  • Is sealed.
  • Cannot contain Instance Constructors(Instance constructors are used to create and initialize any instance member variables when you use the new expression to create an object of a class).
 

 
                                                                                       **      For more details please go through the Link

          

      Monday, 13 January 2014

      What is .NET Framework?


      • .Net Framework is a software Framework developed by Microsoft that runs primarily on Microsoft windows.
      • It Includes large libraries and provides language Interoperability across several programming Language.
      • Programs Written in .Net Framework executes in software environment known as Common Language Run time (CLR).
      • The Class Library and CLR together constitutes the .NET Framework.
      • .NET Framework's Base Class Library provides user interface, data access, database connectivity, cryptography, web application development, numeric algorithms, and network communications. Programmers produce software by combining their own source code with .NET Framework and other libraries.

         Design Features: 

      •  Interoperability
      •  Portability.
      •  Security
      •  Common Language Run time(CLR)
      •  Language Independence.
      •  Base Class Libraries.
       
        Architecture of .Net Framework

        
                Common Language Infrastructure (CLI):  

      • The main purpose if Common Language Infrastructure is to provide a language neutral platform for application development and execution, including function for exception handling, garbage collection, security and interoperability.
      •  By implementing the core aspects of .NET Framework within the scope of the    CLI, this functionality will not be tied to a single language but will be available  across the many languages supported by the framework. Microsoft's  implementation of the CLI is called the Common Language Run time, or CLR. 

                   Diagrammatic Overview of the Common Language Infrastructure
                     

               What is Common Intermediate Language ?         
           
      • During compilation of CLI programming languages, the source code is                   translated into CIL code rather than platform or processor-specific object code.
      • CIL is a CPU- and platform-independent instruction set that can be executed in any environment supporting the Common Language Infrastructure,such as the.NET run-time on Windows, or the cross-platform Mono runtime. 
      • In theory, this eliminates the need to distribute different executable files for different platforms and CPU types. 
      • CIL code is verified for safety during runtime, providing better security and reliability than natively compiled executable files.
           The execution process looks like this:
      • Source code is converted to Common Intermediate Language (CIL), which is the CLI's equivalent to Assembly language for a CPU.
      • CIL is then assembled into a form of so-called bytecode and a CLI assembly is created.
      • Upon execution of a CLI assembly, its code is passed through the run time's JIT compiler to generate native code. Ahead-of-time compilation may also be used, which eliminates this step, but at the cost of executable file portability.
      • The native code is executed by the computer's processor.