c# implicit constructor code example

Example 1: c# class constructor

//The Class
public class MyClass
{
  	//Some Variables
	public readonly int var1;
  	public readonly string var2;
  	//readonly only lets you set it ina constructor	
  
  	//The Constructor (public [class name](parameters))
  	public MyClass(int param1, string param2)
    {
      	//Setting the earlier variables
     	var1 = param1;
      	var2 = param2;
    }
}

//Creating an object using that class
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);

//Output
//10
//This is the second object!

Example 2: c# base class without empty constructor

class A
{
    public A(int x, int y)
    {
        // do something
    }
}

class A2 : A
{
    public A2() : base(1, 5)
    {
        // do something
    }

    public A2(int x, int y) : base(x, y)
    {
        // do something
      	// but no need to repeat the statements from the original constructor
    }

    // This would not compile:
    public A2(int x, int y)
    {
        // the compiler will look for a constructor A(), which doesn't exist
    }
}