Wednesday, February 25, 2015

private constructor

A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class. 
For example:

public class Counter
{
    private Counter() { }
    public static int currentCount;
    public static int IncrementCount()
    {
        return ++currentCount;
    }
}

class TestCounter
{
    static void Main()
    {
        // If you uncomment the following statement, it will generate 
        // an error because the constructor is inaccessible: 
        // Counter aCounter = new Counter();   // Error

        Counter.currentCount = 100;
        Counter.IncrementCount();
        Console.WriteLine("New count: {0}", Counter.currentCount);

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
// Output: New count: 101

 When we declare a class constructor as private , we can not do 2 things:-

1. We can not create a object of the class.
2. We can not inherit the class.

Tuesday, February 24, 2015

Static Constructor C#

Static Constructor:-

 A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.

Example

class SimpleClass
{
    // Static variable that must be initialized at run time.
    static readonly long baseline;

    // Static constructor is called at most one time, before any
    // instance constructor is invoked or member is accessed.
    static SimpleClass()
    {
        baseline = DateTime.Now.Ticks;
    }
}

Static constructors have the following properties:
  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • A static constructor cannot be called directly.
  • The user has no control on when the static constructor is executed in the program.
  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
  • Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
  • If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.

    Thanks & Regards
    Alok Kumar Sharma 

What is Early & Late Binding in C#

Early Binding :-
    Calling a non-Virtual method,decided at a compile time is known as early binding.
Late Binding
    Calling a virtual method (Pure polymorphism),decided at run-time is known as Late binding.

Thanks & Regards
Alok Kumar Sharma

Wednesday, February 18, 2015

.NET Framework Version History

.NET Version
Introduced with IDE 
Features Detail
CLR Version
4.6
Visual Studio 2015
.Introduced RyuJIT, a new JIT compiler for 64-bit systems
.Introduced Open Source.Net Framework Packages
.Support for Code page encodings
.Improvements to event tracing
4.0
4.5.1
Visual Studio 2013
·  Includes performance and debugging improvements
·  Support for automatic binding redirection
·  Expanded support for Windows Store apps
4.0
4.5
Visual Studio 2012
·  Features Enhancements to CLR 4.0
·  Async Support
·  Support for building Windows Store apps
·  Features Enhancement to WPF, WCF, WF, and ASP.NET
4.0
4.0
Visual Studio 2010
·  Introduced CLR 4.0
·  Managed Extensibility Framework (MEF)
·  Dynamic Language Runtime (DLR)
·  Task Parallel Library
4.0
3.5
Visual Studio 2008
·  Built-In AJAX Support
·  LINQ
·  Dynamic Data
·  Multi-targeting Framework Support
2.0
3.0
Visual Studio 2005
·  Windows Presentation Foundation (WPF)
·  Windows Communications Foundation (WCF)
·  Windows Workflow Foundation (WF), and CardSpace
2.0
2.0
Visual Studio 2005
·  Introduced CLR 2.0
·  Generics and generic collections
·  Partial classes
·  Nullable types
·  Anonymous methods
·  Introduced many new controls and features to ASP.NET
2.0
1.1
Visual Studio.NET 2003
·  Features Enhancement to ASP.NET and ADO.NET
·  Built-in support for mobile ASP.NET controls
·  Security Enhancement
·  Built-in support for ODBC and databases
·  Internet Protocol version 6 (IPv6) support
1.0
1.0
Visual Studio.NET
·  Introduced CLR 1.0
·  Support for Object-oriented Web application development
·  Use of DLL class libraries
1.0