Johnathon
0
Q:

parameterized constructor in c#

/*
Parameterized Constructor

A constructor having at least one parameter is called as 
parameterized constructor. It can initialize each instance of the class 
to different values.
*/
// C# Program to illustrate calling of 
// parameterized constructor. 
using System; 
namespace ParameterizedConstructorExample { 

class Geek { 

	// data members of the class. 
	String name; 
	int id; 

	// parameterized constructor would 
	// initialized data members with 
	// the values of passed arguments 
	// while object of that class created. 
	Geek(String name, int id) 
	{ 
		this.name = name; 
		this.id = id; 
	} 

	// Main Method 
	public static void Main() 
	{ 

		// This will invoke parameterized 
		// constructor. 
		Geek geek1 = new Geek("GFG", 1); 
		Console.WriteLine("GeekName = " + geek1.name + 
						" and GeekId = " + geek1.id); 
	} 
} 
} 

//Output:
GeekName = GFG and GeekId = 1
1
//Types of Constructors in C#

1. Default Constructor
2. Parametrized Constructor
3. Copy Constructor
4. Private Constructor
6. Static Constructor
1

New to Communities?

Join the community