SlideShare a Scribd company logo
1 of 21
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

More Related Content

What's hot

TypeScript for Java Developers
TypeScript for Java DevelopersTypeScript for Java Developers
TypeScript for Java DevelopersYakov Fain
 
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakMarcus Denker
 
LINQ Inside
LINQ InsideLINQ Inside
LINQ Insidejeffz
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modelingCodemotion
 
Angular Application Testing
Angular Application TestingAngular Application Testing
Angular Application TestingTroy Miles
 
Spring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffSpring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffJAX London
 
Code generation with javac plugin
Code generation with javac pluginCode generation with javac plugin
Code generation with javac pluginOleksandr Radchykov
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in PharoMarcus Denker
 
使用.NET构建轻量级分布式框架
使用.NET构建轻量级分布式框架使用.NET构建轻量级分布式框架
使用.NET构建轻量级分布式框架jeffz
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Ganesh Samarthyam
 
Reflection in Pharo5
Reflection in Pharo5Reflection in Pharo5
Reflection in Pharo5Marcus Denker
 
JDK8 Functional API
JDK8 Functional APIJDK8 Functional API
JDK8 Functional APIJustin Lin
 
Monitoring distributed (micro-)services
Monitoring distributed (micro-)servicesMonitoring distributed (micro-)services
Monitoring distributed (micro-)servicesRafael Winterhalter
 

What's hot (20)

TypeScript for Java Developers
TypeScript for Java DevelopersTypeScript for Java Developers
TypeScript for Java Developers
 
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond Smalltak
 
4. Interaction
4. Interaction4. Interaction
4. Interaction
 
LINQ Inside
LINQ InsideLINQ Inside
LINQ Inside
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
Angular Application Testing
Angular Application TestingAngular Application Testing
Angular Application Testing
 
Spring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffSpring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard Wolff
 
Code generation with javac plugin
Code generation with javac pluginCode generation with javac plugin
Code generation with javac plugin
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
 
使用.NET构建轻量级分布式框架
使用.NET构建轻量级分布式框架使用.NET构建轻量级分布式框架
使用.NET构建轻量级分布式框架
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams
 
Easy mock
Easy mockEasy mock
Easy mock
 
Java 8 Feature Preview
Java 8 Feature PreviewJava 8 Feature Preview
Java 8 Feature Preview
 
Reflection in Pharo5
Reflection in Pharo5Reflection in Pharo5
Reflection in Pharo5
 
Java concurrency questions and answers
Java concurrency questions and answers Java concurrency questions and answers
Java concurrency questions and answers
 
06.1 .Net memory management
06.1 .Net memory management06.1 .Net memory management
06.1 .Net memory management
 
JDK8 Functional API
JDK8 Functional APIJDK8 Functional API
JDK8 Functional API
 
Eval4j @ JVMLS 2014
Eval4j @ JVMLS 2014Eval4j @ JVMLS 2014
Eval4j @ JVMLS 2014
 
Monitoring distributed (micro-)services
Monitoring distributed (micro-)servicesMonitoring distributed (micro-)services
Monitoring distributed (micro-)services
 

Similar to 25csharp

constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxconstructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxAshrithaRokkam
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its TypesMuhammad Hammad Waseem
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptxurvashipundir04
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdfMadnessKnight
 
ECMA5 approach to building JavaScript frameworks with Anzor Bashkhaz
ECMA5 approach to building JavaScript frameworks with Anzor BashkhazECMA5 approach to building JavaScript frameworks with Anzor Bashkhaz
ECMA5 approach to building JavaScript frameworks with Anzor BashkhazFITC
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Chapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdfChapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdfKavitaHegde4
 
Chapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptxChapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptxKavitaHegde4
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxsasukeman
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorSunipa Bera
 

Similar to 25csharp (20)

Oops
OopsOops
Oops
 
22c
22c22c
22c
 
22csharp
22csharp22csharp
22csharp
 
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxconstructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
 
Constructors in JAva.pptx
Constructors in JAva.pptxConstructors in JAva.pptx
Constructors in JAva.pptx
 
Constructor
ConstructorConstructor
Constructor
 
Core java day4
Core java day4Core java day4
Core java day4
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
 
Constructors
ConstructorsConstructors
Constructors
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
 
ECMA5 approach to building JavaScript frameworks with Anzor Bashkhaz
ECMA5 approach to building JavaScript frameworks with Anzor BashkhazECMA5 approach to building JavaScript frameworks with Anzor Bashkhaz
ECMA5 approach to building JavaScript frameworks with Anzor Bashkhaz
 
Constructor
ConstructorConstructor
Constructor
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
Chapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdfChapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdf
 
Chapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptxChapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptx
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 

More from Sireesh K (20)

Cn10
Cn10Cn10
Cn10
 
chanakya neeti
chanakya neetichanakya neeti
chanakya neeti
 
chanakya neeti
chanakya neetichanakya neeti
chanakya neeti
 
What is mvc
What is mvcWhat is mvc
What is mvc
 
31c
31c31c
31c
 
31cs
31cs31cs
31cs
 
45c
45c45c
45c
 
44c
44c44c
44c
 
43c
43c43c
43c
 
42c
42c42c
42c
 
41c
41c41c
41c
 
40c
40c40c
40c
 
39c
39c39c
39c
 
38c
38c38c
38c
 
37c
37c37c
37c
 
35c
35c35c
35c
 
34c
34c34c
34c
 
33c
33c33c
33c
 
30c
30c30c
30c
 
29c
29c29c
29c
 

Recently uploaded

Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 

25csharp

  • 1. C# Tutorial Part 24:Object and 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 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
  • 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# 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(); • } }
  • 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# 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:
  • 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. • // it will works fine • Sample obj = new Sample("Welcome","to Sirykt"); • // it will not work because of inaccessability • Sample obj=new Sample();
  • 18. • 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
  • 19. • 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!"); }
  • 20. • 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
  • 21. For more visit our website www.siri-kt.blogspot.com Thanks for Watching More Angular JS TutorialsMore C sharp (c#) tutorials