C# Tutorial
Part 24:Object and Class
www.siri-kt.blogspot.com
• In C#, constructor is a special method which is invoked
automatically at the time of object creation. It is used
to initialize the data members of new object generally.
The constructor in C# has the same name as class or
struct.
• Constructor is used allocate the memory. it is used to
initialize components, initialize the variables, opening the
Connections & opening the files.
• Class name & contructor name is the same but prefixed
with public access specifier.
• One class supports multiple constructors.
• Constructor is supports overloading but does not accepts
any return types. Constructor is also called as
parameterized constructor.
• Types of Constructors
• Basically constructors are 5 types those are
• 1. Default Constructor
• 2. Parameterized Constructor
• 3. Copy Constructor
• 4. Static Constructor
• 5. Private Constructor
•
• Default Constructor
• A constructor which has no argument is known as default
constructor. It is invoked at the time of creating object.
• A constructor without having any parameters called default
constructor. In this constructor every instance of the class will be
initialized without any parameter values like as shown below
• using System;
• namespace ConsoleApplication3
• { class Sample
• { public string param1, param2;
• public Sample() // Default Constructor
• { param1 = "Welcome";
• param2 = “Siri’s";} }
• class Program
• { static void Main(string[] args)
• { Sample obj=new Sample(); // Once object of class created automatically
constructor will be called
• Console.WriteLine(obj.param1);
• Console.WriteLine(obj.param2);
• Console.ReadLine();
• }} }
• Output: Welcome
• Siri’s
• C# Default Constructor Example: Having Main() in another
class
• Let's see another example of default constructor where we
are having Main() method in another class.
• using System;
• public class Employee
• { public Employee()
• { Console.WriteLine("Default Constructor Invok
ed"); } }
• class TestEmployee{
• public static void Main(string[] args)
• { Employee e1 = new Employee();
• Employee e2 = new Employee();
• } }
• Parameterized Constructors
• A constructor with at least one parameter is called as
parameterized constructor. In parameterized constructor we
can initialize each instance of the class to different values like
as shown below
• using System;
• namespace ConsoleApplication3
• { class Sample
• { public string param1, param2;
• public Sample(string x, string y) // Declaring Parameterized
constructor with Parameters
• { param1 = x;
• param2 = y;
• } }
• class Program
• { static void Main(string[] args)
• { Sample obj=new Sample("Welcome",“Siri’s"); // Parameterized
Constructor Called
• Console.WriteLine(obj.param1 +" to "+ obj.param2);
• Console.ReadLine();
• } } }
• Output: Welcome to Siri’s
• C# Parameterized Constructor
• A constructor which has parameters is called parameterized constructor. It is used to provide
different values to distinct objects.
• using System;
• public class Employee
• { public int id;
• public String name;
• public float salary;
• public Employee(int i, String n,float s)
• { id = i;
• name = n;
• salary = s; }
• public void display()
• { Console.WriteLine(id + " " + name+" "+salary); } }
• class TestEmployee{
• public static void Main(string[] args)
• { Employee e1 = new Employee(101, "Sonoo", 890000f);
• Employee e2 = new Employee(102, "Mahesh", 490000f);
• e1.display();
• e2.display(); } }
• Output:
• Constructor Overloading
• In c# we can overload constructor by creating
another constructor with same method name and
different parameters like as shown below
• using System;
• namespace ConsoleApplication3
• { class Sample
• { public string param1, param2;
• public Sample() // Default Constructor
• { param1 = "Hi";
• param2 = "I am Default Constructor";}
• public Sample(string x, string y) // Declaring Parameterized constructor with Parameters
• { param1 = x;
• param2 = y;} }
• class Program
• { static void Main(string[] args)
• { Sample obj = new Sample(); // Default Constructor will Called
• Sample obj1=new Sample("Welcome",“Siri’s"); // Parameterized Constructor will Called
• Console.WriteLine(obj.param1 + ", "+obj.param2);
• Console.WriteLine(obj1.param1 +" to " + obj1.param2);
• Console.ReadLine();} }
• Output:Hi, I am Default Constructor
• Welcome to Siri’s
• 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.
Check below example for this
• using System;
• namespace ConsoleApplication3
• { class Sample
• { public string param1, param2;
• public Sample(string x, string y)
• { param1 = x;
• param2 = y;
• }
• public Sample(Sample obj) // Copy Constructor
• { param1 = obj.param1;
• param2 = obj.param2;
• } }
• class Program
• { static void Main(string[] args)
• { Sample obj = new Sample("Welcome", “Sirykt"); // Create instance to class
Sample
• Sample obj1=new Sample(obj); // Here obj details will copied to obj1
• Console.WriteLine(obj1.param1 +" to " + obj1.param2);
• Console.ReadLine();
• } } }
• Output: Welcome to Sirykt
• Static Constructor
• When we declared constructor as static it will be invoked
only once for any number of instances of the class and it’s
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.
• Importance points of static constructor
• Static constructor will not accept any parameters because
it is automatically called by CLR.
• Static constructor will not have any access modifiers.
• Static constructor will execute automatically whenever we
create first instance of class
• Only one static constructor will allowed.
• using System;
• namespace ConsoleApplication3
• { class Sample
• { public string param1, param2;
• static Sample()
• { Console.WriteLine("Static Constructor"); }
• public Sample()
• { param1 = "Sample";
• param2 = "Instance Constructor"; } }
• class Program
• { static void Main(string[] args)
• { // Here Both Static and instance constructors are invoked for first instance
• Sample obj=new Sample();
• Console.WriteLine(obj.param1 + " " + obj.param2);
• // Here only instance constructor will be invoked
• Sample obj1 = new Sample();
• Console.WriteLine(obj1.param1 +" " + obj1.param2);
• Console.ReadLine(); } } }
• Output:Static Constructor
• Sample Instance Constructor
• Sample Instance Constructor
• Private Constructor
• Private constructor is a special instance
constructor used in a class that contains static
member only.
• If a class has one or more private constructor and
no public constructor then other classes is not
allowed to create instance of this class this mean
we can neither create the object of the class nor it
can be inherit by other class.
• The main purpose of creating private constructor is
used to restrict the class from being instantiated
• using System;
• namespace ConsoleApplication3
• { public class Sample
• { public string param1, param2;
• public Sample(string a,string b)
• { param1 = a;
• param2 = b;
• }
• private Sample() // Private Constructor Declaration
• { Console.WriteLine("Private Constructor with no prameters");
• } }
• class Program
• { static void Main(string[] args)
• { // Here we don't have chance to create instace for private constructor
• Sample obj = new Sample("Welcome","to sirykt");
• Console.WriteLine(obj.param1 +" " + obj.param2);
• Console.ReadLine();
• } } }
• Output: Welcome to sirykt
• // it will works fine
• Sample obj = new Sample("Welcome","to Sirykt");
• // it will not work because of inaccessability
• Sample obj=new Sample();
• Important points of private constructor
• One use of private construct is when we have only
static member.
• Once we provide a constructor that is either
private or public or any, the compiler will not allow
us to add public constructor without parameters to
the class.
• If we want to create object of class even if we
have private constructors then we need to have
public constructor along with private constructor
• Here is an example:
• public Car() { }
• public Car(string color) { this.color = color; }
• A constructor can call another constructor, which
can come in handy in several situations. Here is an
example:
• public Car() {
• Console.WriteLine("Constructor with no
parameters called!"); }
• public Car(string color) : this() {
• this.color = color; Console.WriteLine("Constructor
with color parameter called!"); }
• If you run this code, you will see that the constructor with
no parameters is called first.
• This can be used for instantiating various objects for the
class in the default constructor, which can be called from
other constructors from the class.
• If the constructor you wish to call takes parameters, you
can do that as well.
• Here is a simple example:
• public Car(string color) : this()
• { this.color = color;
• Console.WriteLine("Constructor with color parameter
called!"); }
• public Car(string param1, string param2) : this(param1) { }
• If you call the constructor which takes 2 parameters, the
For more visit our website www.siri-kt.blogspot.com
Thanks for
Watching
More Angular JS TutorialsMore C sharp (c#) tutorials

25csharp

  • 1.
    C# Tutorial Part 24:Objectand Class www.siri-kt.blogspot.com
  • 2.
    • In C#,constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C# has the same name as class or struct. • Constructor is used allocate the memory. it is used to initialize components, initialize the variables, opening the Connections & opening the files. • Class name & contructor name is the same but prefixed with public access specifier. • One class supports multiple constructors. • Constructor is supports overloading but does not accepts any return types. Constructor is also called as parameterized constructor.
  • 3.
    • Types ofConstructors • Basically constructors are 5 types those are • 1. Default Constructor • 2. Parameterized Constructor • 3. Copy Constructor • 4. Static Constructor • 5. Private Constructor • • Default Constructor • A constructor which has no argument is known as default constructor. It is invoked at the time of creating object. • A constructor without having any parameters called default constructor. In this constructor every instance of the class will be initialized without any parameter values like as shown below
  • 4.
    • using System; •namespace ConsoleApplication3 • { class Sample • { public string param1, param2; • public Sample() // Default Constructor • { param1 = "Welcome"; • param2 = “Siri’s";} } • class Program • { static void Main(string[] args) • { Sample obj=new Sample(); // Once object of class created automatically constructor will be called • Console.WriteLine(obj.param1); • Console.WriteLine(obj.param2); • Console.ReadLine(); • }} } • Output: Welcome • Siri’s
  • 5.
    • C# DefaultConstructor Example: Having Main() in another class • Let's see another example of default constructor where we are having Main() method in another class. • using System; • public class Employee • { public Employee() • { Console.WriteLine("Default Constructor Invok ed"); } } • class TestEmployee{ • public static void Main(string[] args) • { Employee e1 = new Employee(); • Employee e2 = new Employee(); • } }
  • 6.
    • Parameterized Constructors •A constructor with at least one parameter is called as parameterized constructor. In parameterized constructor we can initialize each instance of the class to different values like as shown below
  • 7.
    • using System; •namespace ConsoleApplication3 • { class Sample • { public string param1, param2; • public Sample(string x, string y) // Declaring Parameterized constructor with Parameters • { param1 = x; • param2 = y; • } } • class Program • { static void Main(string[] args) • { Sample obj=new Sample("Welcome",“Siri’s"); // Parameterized Constructor Called • Console.WriteLine(obj.param1 +" to "+ obj.param2); • Console.ReadLine(); • } } } • Output: Welcome to Siri’s
  • 8.
    • C# ParameterizedConstructor • A constructor which has parameters is called parameterized constructor. It is used to provide different values to distinct objects. • using System; • public class Employee • { public int id; • public String name; • public float salary; • public Employee(int i, String n,float s) • { id = i; • name = n; • salary = s; } • public void display() • { Console.WriteLine(id + " " + name+" "+salary); } } • class TestEmployee{ • public static void Main(string[] args) • { Employee e1 = new Employee(101, "Sonoo", 890000f); • Employee e2 = new Employee(102, "Mahesh", 490000f); • e1.display(); • e2.display(); } } • Output:
  • 9.
    • Constructor Overloading •In c# we can overload constructor by creating another constructor with same method name and different parameters like as shown below
  • 10.
    • using System; •namespace ConsoleApplication3 • { class Sample • { public string param1, param2; • public Sample() // Default Constructor • { param1 = "Hi"; • param2 = "I am Default Constructor";} • public Sample(string x, string y) // Declaring Parameterized constructor with Parameters • { param1 = x; • param2 = y;} } • class Program • { static void Main(string[] args) • { Sample obj = new Sample(); // Default Constructor will Called • Sample obj1=new Sample("Welcome",“Siri’s"); // Parameterized Constructor will Called • Console.WriteLine(obj.param1 + ", "+obj.param2); • Console.WriteLine(obj1.param1 +" to " + obj1.param2); • Console.ReadLine();} } • Output:Hi, I am Default Constructor • Welcome to Siri’s
  • 11.
    • 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. Check below example for this
  • 12.
    • using System; •namespace ConsoleApplication3 • { class Sample • { public string param1, param2; • public Sample(string x, string y) • { param1 = x; • param2 = y; • } • public Sample(Sample obj) // Copy Constructor • { param1 = obj.param1; • param2 = obj.param2; • } } • class Program • { static void Main(string[] args) • { Sample obj = new Sample("Welcome", “Sirykt"); // Create instance to class Sample • Sample obj1=new Sample(obj); // Here obj details will copied to obj1 • Console.WriteLine(obj1.param1 +" to " + obj1.param2); • Console.ReadLine(); • } } } • Output: Welcome to Sirykt
  • 13.
    • Static Constructor •When we declared constructor as static it will be invoked only once for any number of instances of the class and it’s 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. • Importance points of static constructor • Static constructor will not accept any parameters because it is automatically called by CLR. • Static constructor will not have any access modifiers. • Static constructor will execute automatically whenever we create first instance of class • Only one static constructor will allowed.
  • 14.
    • using System; •namespace ConsoleApplication3 • { class Sample • { public string param1, param2; • static Sample() • { Console.WriteLine("Static Constructor"); } • public Sample() • { param1 = "Sample"; • param2 = "Instance Constructor"; } } • class Program • { static void Main(string[] args) • { // Here Both Static and instance constructors are invoked for first instance • Sample obj=new Sample(); • Console.WriteLine(obj.param1 + " " + obj.param2); • // Here only instance constructor will be invoked • Sample obj1 = new Sample(); • Console.WriteLine(obj1.param1 +" " + obj1.param2); • Console.ReadLine(); } } } • Output:Static Constructor • Sample Instance Constructor • Sample Instance Constructor
  • 15.
    • Private Constructor •Private constructor is a special instance constructor used in a class that contains static member only. • If a class has one or more private constructor and no public constructor then other classes is not allowed to create instance of this class this mean we can neither create the object of the class nor it can be inherit by other class. • The main purpose of creating private constructor is used to restrict the class from being instantiated
  • 16.
    • using System; •namespace ConsoleApplication3 • { public class Sample • { public string param1, param2; • public Sample(string a,string b) • { param1 = a; • param2 = b; • } • private Sample() // Private Constructor Declaration • { Console.WriteLine("Private Constructor with no prameters"); • } } • class Program • { static void Main(string[] args) • { // Here we don't have chance to create instace for private constructor • Sample obj = new Sample("Welcome","to sirykt"); • Console.WriteLine(obj.param1 +" " + obj.param2); • Console.ReadLine(); • } } } • Output: Welcome to sirykt
  • 17.
    • // itwill works fine • Sample obj = new Sample("Welcome","to Sirykt"); • // it will not work because of inaccessability • Sample obj=new Sample();
  • 18.
    • Important pointsof private constructor • One use of private construct is when we have only static member. • Once we provide a constructor that is either private or public or any, the compiler will not allow us to add public constructor without parameters to the class. • If we want to create object of class even if we have private constructors then we need to have public constructor along with private constructor
  • 19.
    • Here isan example: • public Car() { } • public Car(string color) { this.color = color; } • A constructor can call another constructor, which can come in handy in several situations. Here is an example: • public Car() { • Console.WriteLine("Constructor with no parameters called!"); } • public Car(string color) : this() { • this.color = color; Console.WriteLine("Constructor with color parameter called!"); }
  • 20.
    • If yourun this code, you will see that the constructor with no parameters is called first. • This can be used for instantiating various objects for the class in the default constructor, which can be called from other constructors from the class. • If the constructor you wish to call takes parameters, you can do that as well. • Here is a simple example: • public Car(string color) : this() • { this.color = color; • Console.WriteLine("Constructor with color parameter called!"); } • public Car(string param1, string param2) : this(param1) { } • If you call the constructor which takes 2 parameters, the
  • 21.
    For more visitour website www.siri-kt.blogspot.com Thanks for Watching More Angular JS TutorialsMore C sharp (c#) tutorials