Use this and base together to init a class : Member Variable « Class « C# / CSharp Tutorial






using System;

class Base
{
   public Base( int x )
   {
      Console.WriteLine( "Base.Base(int)" );
      this.x = x;
   }
   
   public int x = 0;
}

class Derived : Base
{
   public Derived( int a ):base( a )
   {
      Console.WriteLine( "Derived.Derived(int)" );
      this.a = a;
   }

   public Derived( int a, int b ):this( a )
   {
      Console.WriteLine( "Derived.Derived(int, int)" );
      this.a = a;
      this.b = b;
   }

   public int a = 0;
   public int b = 0;
}

public class MainClass
{
   static void Main()
   {
      Derived b = new Derived( 1, 2 );
   }
}
Base.Base(int)
Derived.Derived(int)
Derived.Derived(int, int)








7.5.Member Variable
7.5.1.fields
7.5.2.A class with method and member variables
7.5.3.field initialization
7.5.4.Add a method to access the field variables
7.5.5.Call base constructor to init member variables
7.5.6.Use this and base together to init a class
7.5.7.How to use a 'has a' relationship
7.5.8.Illustrates how to assign default values to fields using initializers