C# program to demonstrate the use of constructor
Program.cs
Console.WriteLine("———————————————————————————————————————————");
Console.WriteLine("Program to use a constructor");
Console.WriteLine("———————————————————————————————————————————");
Computer c1 = new Computer();
Computer c2 = new Computer("Wireless", "Lenovo", 145221);
c2.display();
GC.Collect();
Console.WriteLine("———————————————————————————————————————————");

public class Computer
{
    public String mouseType = "";
    public String mouseCmp = "";
    public int mouseId;

    public Computer()
    {
        Console.WriteLine("Constructor");
    }
    public Computer(String t, String c, int i)
    {
        mouseType = t;
        mouseCmp = c;
        mouseId = i;
    }
    public void display()
    {
        Console.WriteLine("Mouse Type: " + mouseType + "\nMouse Cmp:  " + mouseCmp + "\nMouse Id:   " + mouseId);
    }
}
Output
kodingwindow@kw:~/csharp$ dotnet run
———————————————————————————————————————————
Program to use a constructor
———————————————————————————————————————————
Constructor
Mouse Type: Wireless
Mouse Cmp:  Lenovo
Mouse Id:   145221
———————————————————————————————————————————
kodingwindow@kw:~/csharp$ 
Advertisement