C#

Click below links for examples and download  complete codes
1. Click to Power Zoomer examples
2.Example2
3.Image Popup


Difference between Hashtable and Dictionary c# ?

Hashtable and Dictionary are collection of data structures to hold data as key-value pairs. Dictionary is generic type, hash table is not a generic type. The Hashtable is a weakly typed data structure, so you can add keys and values of any Object Type to the Hashtable. The Dictionary class is a strongly types < T Key, T Value > and you must specify the data types for both the key and value.
C# Declaration


Hashtable numbers = new Hashtable();
Dictionary<int, string> dictionary = new Dictionary<int, string >();


Hashtable numbers = new Hashtable();
            numbers.Add(1, "one");
            numbers.Add(2, "two");
            numbers.Add(3, "three");
            foreach (DictionaryEntry num in numbers)
            {
                MessageBox.Show(num.Key + "   -   " + num.Value);
            }
            Dictionary<int, string> dictionary = new Dictionary<int, string >();
            dictionary.Add(1,"one");
            dictionary.Add(2,"two");
            dictionary.Add(3,"three");
            foreach (KeyValuePair<int,string> pair in dictionary)
            {
                MessageBox.Show(pair.Key + "   -   " + pair.Value);
            }

Ques : Clone Examples ?

string[] array = { "dot", "net", "perls" };
string[] cloned = array.Clone() as string[];

Console.WriteLine(string.Join(",", array));
Console.WriteLine(string.Join(",", cloned));

 MyContainer con1 = new MyContainer();

    MyContainer con2 = (MyContainer)con1.Clone();


Constructor types with example programs in C#.NET

A special method of the class that will be automatically invoked when an instance of the class is created is called as constructor.

Constructors can be classified into 5 types
  1. Default Constructor
  2. Parameterized Constructor
  3. Copy Constructor
  4. Static Constructor
  5. Private Constructor
Default Constructor : A constructor without any parameters is called as default constructor. Drawback of default constructor is every instance of the class will be initialized to same values and it is not possible to initialize each instance of the class to different values.

Example for Default Constructor

Parameterized Constructor :
A constructor with at least one parameter is called as parameterized constructor. Advantage of parameterized constructor is you can initialize each instance of the class to different values.


Example for Parameterized Constructor
using System;

namespace ProgramCall
{
    
class Test1
    {
        
//Private fields of class
        
int A, B;

        
//default Constructor
        
public Test1()
        {
            A = 10;
            B = 20;
        }

        
//Paremetrized Constructor
        
public Test1(int X, int Y)
        {
            A = X;
            B = Y;
        }



        
//Method to print
        
public void Print()
        {

            
Console.WriteLine("A  =  {0}\tB  =  {1}", A, B);
        }
    
            
    }


    
class MainClass
    {

        
static void Main()
        {

            
Test1 T1 = new Test1();  //Default Constructor is called
            
Test1 T2 = new Test1(80, 40); //Parameterized Constructor is called
      
            T1.Print();
            T2.Print();

            
Console.Read();
      
        }
    }

}

Output

A  =  10        B  =  20
A  =  80        B  =  40

Copy Constructor : A parameterized constructor that contains a parameter of same class type is called as copy constructor. Main purpose of copy constructor is to initialize new instance to the values of an existing instance.

Example for Copy Constructor
using System;

namespace ProgramCall
{

    
class Test2
    {

        
int A, B;

        
public Test2(int X, int Y)
        {
            A = X;
            B = Y;
        }

        
//Copy Constructor
        
public Test2(Test2 T)
        {
            A = T.A;

            B = T.B;
        }


   
        
public void Print()
        {

            
Console.WriteLine("A  =  {0}\tB  =  {1}", A, B);
        }
 

    }


    
class CopyConstructor
    {
        
static void Main()
        {
       

            
Test2 T2 = new Test2(80, 90);

            
//Invoking copy constructor
            
Test2 T3 = new Test2(T2);
        
            T2.Print();
            T3.Print();

            
Console.Read();
        }
    }
}


Output

A  =  80        B  =  90
A  =  80        B  =  90

Static Constructor : You can create a constructor as static and when a constructor is created as static, it will be invoked only once for any number of instances of the class and it is during the creation of first instance of the class or the first reference to a static member in the class. Static constructor is used to initialize static fields of the class and to write the code that needs to be executed only once.

Example for Static Constructor
using System;

namespace ProgramCall
{
    
class Test3
    {

        
public Test3()
        {

            
Console.WriteLine("Instance  Constructor");
        }

        
static Test3()
        {
            
Console.WriteLine("Static  Constructor");
        }
    }
    
class StaticConstructor
    {
        
static void Main()
        {
            
//Static Constructor and instance constructor, both are invoked for first instance.
            
Test3 T1 = new Test3();

            
//Only instance constructor is invoked.
            
Test3 T2 = new Test3();
 

            
Console.Read();
        }
    }
}


Output

Static  Constructor
Instance  Constructor
Instance  Constructor
Private Constructor : You can also create a constructor as private. When a class contains at least one private constructor, then it is not possible to create an instance for the class. Private constructor is used to restrict the class from being instantiated when it contains every member as static.

Some unique points related to constructors are as follows
  • A class can have any number of constructors.
  • A constructor doesn’t have any return type even void.
  • A static constructor can not be a parameterized constructor.
  • Within a class you can create only one static constructor.





Static Members Vs Instance Members in C#.NET


The members of a class that can not be accessed without creating an instance for the class are called as instance members and the members of a class that can be accessed without creating an instance and directly by using class name are called as static members.

To make a member as static member, declare the member using the keyword static. Static members can not accessed by using an instance.  Indexers and destructor can not be static.


Static Fields : When a field is declared as static then that field will exists only one copy for any number of instances of the class. Hence static fields are used to store the data that is to be shared by all instances of the class. For example, if you want to maintain a count of how many instances are created for the class then only alternative is static field.


Static Methods : When a method is declared as static then that method can access only other static members available in the class and it is not possible to access instance members


Static Classes :
You can also create a class it self as static in c#.net 2005. When a class contains every member as static then there is no meaning in creating an instance for the class. In this case to restrict the class from being instantiated, class can be declared as static class.