C # syntax exercise (11): class [3]-constructor, destructor, base, and this

Source: Internet
Author: User
Constructor and destructor:
Using system; Class myclass {private int fnum; Public int num {get {return fnum ;}}/* the constructor has no return value, the default */Public myclass () {This. fnum = 2009;}/* can have multiple constructors with different parameters */Public myclass (int x) {This. fnum = x;} public myclass (Params int [] ARR) {foreach (int I in ARR) This. fnum + = I;}/* The Destructor has no parameters, no return values, no access modifier, and can only have one */~ Myclass () {// destructor is automatically called} class program {static void main () {myclass obj1, obj2, obj3; obj1 = new myclass (); console. writeline (obj1.num); // 2009 obj2 = new myclass (100); console. writeline (obj2.num); // 100 obj3 = new myclass (1, 2, 3); console. writeline (obj3.num); // 6 console. readkey ();}}
 

If there is no constructor or destructor, the default (or inheritance) will be used for new. A private constructor can prevent the class from being instantiated:

using System;class MyClass{    private MyClass() { }    public static void Msg1() { Console.WriteLine("Msg1"); }    public static void Msg2() { Console.WriteLine("Msg2"); }}class Program{    static void Main()    {        MyClass.Msg1(); //Msg1        MyClass.Msg2(); //Msg2        Console.ReadKey();    }}
 

If a class has a non-default constructor, you cannot use the default constructor:

using System;class MyClass{    private int FNum;    public int Num { get { return FNum; } }    public MyClass(int x, int y)    {        this.FNum = x + y;    }}class Program{    static void Main()    {        MyClass obj;        obj = new MyClass(1, 2);        Console.WriteLine(obj.Num); //3        Console.ReadKey();    }}
 

Static constructor:

The static constructor has no access modifier or parameter;
The static constructor is automatically called before new or any static member is called;
Static constructors are generally used to initialize static data;
A static constructor is triggered before the first new or first use of a static member;
Static constructor cannot be called directly.

using System;class MyClass{    public static int Num;    public static void ShowNum() { Console.WriteLine(Num); }    public void Msg() { Console.WriteLine("Msg"); }    static MyClass() { Num = 123; }}class Program{    static void Main()    {        MyClass.ShowNum();            //123        MyClass.Num = 2009;        MyClass.ShowNum();            //2009        MyClass obj1 = new MyClass();        obj1.Msg();                   //Msg        Console.ReadKey();    }}
 

Automatically call the constructor of the parent class:

using System;class Parent{    public Parent() { Console.WriteLine("Parent"); }}class Child1 : Parent{    public Child1() { Console.WriteLine("Child1"); }    public Child1(int x) { Console.WriteLine(x); }}class Child2 : Child1{    public Child2() { Console.WriteLine("Child2"); }    public Child2(int x, int y) { Console.WriteLine(x + y); }}class Program{    static void Main()    {        Parent p = new Parent();           // Parent        Child1 c1 = new Child1();          // Parent / Child1        Child2 c2 = new Child2();          // Parent / Child1 / Child2        Child1 c11 = new Child1(999);      // Parent / 999        Child2 c22 = new Child2(111, 222); // Parent / Child1 / 333                Console.ReadKey();    }}
 

Base:

using System;class Parent{    public Parent() { Console.WriteLine("Parent"); }    public Parent(int x, int y) { Console.WriteLine(x + y); }    public Parent(string s1, string s2) { Console.WriteLine(s1 + s2); }}class Child1 : Parent{    public Child1() { Console.WriteLine("Child1"); }}class Child2 : Parent{    public Child2() : base() { Console.WriteLine("Child2"); }}class Child3 : Parent{    public Child3() : base(111,222) { Console.WriteLine("Child3"); }}class Child4 : Parent{    public Child4() : base("111", "222") { Console.WriteLine("Child4"); }}class Program{    static void Main()    {        Child1 c1 = new Child1(); // Parent / Child1        Child2 c2 = new Child2(); // Parent / Child2        Child3 c3 = new Child3(); // 333 / Child3        Child4 c4 = new Child4(); // 111222 / Child4                Console.ReadKey();    }}
 

This:

using System;class MyClass{    private string fs = "ABC-";    public MyClass() { Console.WriteLine("MyClass"); }    public MyClass(string str) { Console.WriteLine(this.fs + str); }    public MyClass(int num) : this() { Console.WriteLine(num); }    public MyClass(int x, int y) : this("XYZ") { Console.WriteLine(x + y); }}class Program{    static void Main()    {        MyClass c1 = new MyClass();          // MyClass        MyClass c2 = new MyClass("EFG");     // ABC-EFG        MyClass c3 = new MyClass(123);       // MyClass / 123        MyClass c4 = new MyClass(111, 222);  // ABC-XYZ / 333        Console.ReadKey();    }}
 

Constructors, attributes, and base:

using System;abstract class Parent{    private byte FID;    public Parent(byte n)    {        FID = n;    }    public byte Id    {        get { return FID; }        set { FID = value; }    }}class Child : Parent{    public Child(byte MyID) : base(MyID) { }}class Program{    static void Main()    {        Child Rect = new Child(6);        Console.WriteLine(Rect.Id); //6        Rect.Id = 8;        Console.WriteLine(Rect.Id); //8        Console.ReadKey();    }}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.