how to make a constructor in c# code example
Example 1: c# constructor
public class Person
{
private string last;
private string first;
public Person(string lastName, string firstName)
{
last = lastName;
first = firstName;
}
}
Example 2: c# class constructor
public class MyClass
{
public readonly int var1;
public readonly string var2;
public MyClass(int param1, string param2)
{
var1 = param1;
var2 = param2;
}
}
MyClass Class1 = new MyClass(10, "Hello, World!");
MyClass Class2 = new MyClass(9999, "This is the second object!");
Console.WriteLine(Class1.var1);
Console.WriteLine(Class2.var2);
Example 3: Constructor in C#
class Car
{
public Car(string Name, string Model, int Seat_Number, int Door_Number, double Price)
{
name = Name;
model = Model;
seat_Number = Seat_Number;
door_Number = Door_Number;
price = Price;
}
}
Example 4: how to start a constructer c#
public static void Main()
{