Parameterized Constructor c#.net and class diagram with example program code
Parameterized Constructor:
Here dotnetportal9.blogspot.in blog gives you info about Parameterized Constructor c#.net and class diagram and also we will give beautiful example program to understand Parameterized Constructor easily
what is Parameterized Constructor ?
Here dotnetportal9.blogspot.in blog gives you info about Parameterized Constructor c#.net and class diagram and also we will give beautiful example program to understand Parameterized Constructor easily
what is Parameterized Constructor ?
- This constructor is used to pass the required values to the data fields of a class.
- So that separate data can be stored for each object of the class is created.
- A class can contain multiple Parameterized constructors but from one constructor to another constructor we should make the differentiate with the number of parameters.
- That means if the first constructor is having one parameter than second constructor should have two parameters (or) three parameters and so on.
- Example with Parameterized Constructor.
Class diagram:
Employee3
|
Int Empid
String Ename
String Eadderse
Int Age
|
Public Emplyee3(Int id,String s1,String s2,Int Age)
Public void DisplayEmpData()
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CAConstructors
{
class employee3
{
int Empid, Eage;
string Ename, Edesignation;
public employee3(int id,string s1,string s2,int age)
{
this.Empid = id;
this.Ename = s1;
this.Edesignation = s2;
this.Eage = age;
}
public void DisplayEmpdata()
{
Console.WriteLine("Employee id is :-" + Empid);
Console.WriteLine("Employee Name is :-" + Ename);
Console.WriteLine("Employee Designation is :-" + Edesignation);
Console.WriteLine("Employee age is :-" + Eage);
}
}
class Pconstructor
{
static void Main(string[] args)
{
employee3 obj1 = new employee3(101,"srikanth","programer",26);
employee3 obj2 = new employee3(102,"sai","TL",29);
obj1.DisplayEmpdata();
obj2.DisplayEmpdata();
Console.ReadLine();
}
}
}
OutPut:
A class can contain multiple parameterized constructor but from one constructor to another constructor we should differentiate with in the parameter.
- In the above example if we want to create an object to empoyee3 class like
employee3 obj1=new employee3();
- then it is not possible and raises compilation error.And like employee3 does not contain a constructor that takes zero arguments.
- To overcome this we should write a constructor in the class with number of arguments like below
public emloyee3()
{
this.Empid = 102;
this.Ename = "sai";
this.Edesignation = "TL";
this.Eage = 26;
}
It is also not possible to create an object like below
employee3 obj2=new employee3(104,"sri");
- This will rise a compilation error like:-employee3 does not contain a constructor that takes 2 arguments.
- To overcome this we should write a constructor with two arguments like
public empoyee3(int id,string s1)
{
this.empid=Id;
this.ename=s1;
}
After writing these two constructors in the class,the class will be like
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CAConstructors
{
class employee3
{
int Empid, Eage;
string Ename, EAddress;
public employee3()
{
this.Empid = 110;
this.Ename = "sri";
this.EAddress = "Hyderabad";
this.Eage = 26;
}
public employee3(int id,string s1)
{
this.Empid = id;
this.Ename = s1;
}
public employee3(int id,string s1,string s2,int age)
{
this.Empid = id;
this.Ename = s1;
this.EAddress = s2;
this.Eage = age;
}
public void DisplayEmpdata()
{
Console.WriteLine("Employee id is :-" + Empid);
Console.WriteLine("Employee Name is :-" + Ename);
Console.WriteLine("Employee Address is :-" + EAddress);
Console.WriteLine("Employee age is :-" + Eage);
}
}
class Pconstructor
{
static void Main(string[] args)
{
employee3 obj1 = new employee3();
employee3 obj2 = new employee3(102,"sai");
employee3 obj3 = new employee3(103, "srikanth","Hyderabad",28);
obj1.DisplayEmpdata();
obj2.DisplayEmpdata();
obj3.DisplayEmpdata();
Console.ReadLine();
}
}
}
OutPut:-
After writing these two constructors in the class,the class will be like
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CAConstructors
{
class employee3
{
int Empid, Eage;
string Ename, EAddress;
public employee3()
{
this.Empid = 110;
this.Ename = "sri";
this.EAddress = "Hyderabad";
this.Eage = 26;
}
public employee3(int id,string s1)
{
this.Empid = id;
this.Ename = s1;
}
public employee3(int id,string s1,string s2,int age)
{
this.Empid = id;
this.Ename = s1;
this.EAddress = s2;
this.Eage = age;
}
public void DisplayEmpdata()
{
Console.WriteLine("Employee id is :-" + Empid);
Console.WriteLine("Employee Name is :-" + Ename);
Console.WriteLine("Employee Address is :-" + EAddress);
Console.WriteLine("Employee age is :-" + Eage);
}
}
class Pconstructor
{
static void Main(string[] args)
{
employee3 obj1 = new employee3();
employee3 obj2 = new employee3(102,"sai");
employee3 obj3 = new employee3(103, "srikanth","Hyderabad",28);
obj1.DisplayEmpdata();
obj2.DisplayEmpdata();
obj3.DisplayEmpdata();
Console.ReadLine();
}
}
}
OutPut:-
- From this we can say that a class can contain any number of constructors.
- A constructor can be overloaded.
0 comments:
Post a Comment