C # Fundamentals collation: C # classes and structs (2)

Source: Internet
Author: User
   1. What is a constructor function? What are the constructors? What are the definitions, implementation methods, and considerations for each constructor?
The so-called constructor is a method that initializes the object, that is, after the function is run, the memory always opens up a space for the object of the class. There are three kinds: normal constructors, that is, instantiated constructors; private constructors; static constructors.
Instantiate the constructor:

    public class Example    {        private string property1 = String. Empty;        private string property2 = @ "Hello";        private int property3 = 0;        The public Example ()//member is the initial value when declaring, and this default constructor can also be not written.        {        } public        Example (string p1, string p2, int p3)//The value passed in initializes        {            this.property1 = p1;            This.property2 = p2;            This.property3 = p3;        }    }

Private constructors:
Private constructor, external is inaccessible, then how to instantiate it, see Singleton mode, here is the use of private constructors:

http://www.php.cn/

Static constructors:
First look at the example:

public class Staticconstruct    {static        staticconstruct ()        {            Console.WriteLine (@ "Static constructor");        }        Public staticconstruct ()        {            Console.WriteLine (@ "materialized constructor");        }        Public staticconstruct (String flage)        {            Console.WriteLine (@ "with parameter constructor");}    }    Class program    {        static void Main (string[] args)        {            staticconstruct strc = new Staticconstruct ();            Staticconstruct strcvalue = new Staticconstruct (string. Empty);            Console.ReadLine ();        }    }

Results:

Static constructor Features: Access modifiers are not allowed in static constructors; When instantiated, the static constructor is called automatically, meaning that calling the static constructor is not controllable; The static constructor is non-parametric and only one of the classes is not inherited.
 2. What is this keyword and base keyword used for? Implementation code?
(1), this keyword:
This refers to the meaning in this class, referencing the members of the current class. Of course, if the program is running, you can say precisely that this refers to the members of the object of the current class, which is used to distinguish objects. Because a class can have n objects. However, in the static class can not use the This keyword, the reason is that static can not be instantiated more than one object, it only one, there is no need to use this to distinguish objects. Generally used as follows:
A, method, or constructor, with the same name variable.

     public class Mytesta    {        private string TestA = String. Empty;        Public Mytesta (String TestA)        {            this.testa = TestA;        }        public void Handler (string testA)        {            this.testa = TestA;        }    }

B, Get,set method

    public class Mytestb    {        private string testb = String. Empty;        public string Testb        {            get             {                 return this.testb;            }            Set             {                 this.testb = value;}}    }

C. Passing an instance
For example, in the event

    public class MYTESTC {public event EventHandler ontestcevent = null;                private void Send_ontestevent (Object Sender,eventargs e) {if (ontestcevent! = null) {            Ontestcevent (sender, E);        }} private void TestEvent () {send_ontestevent (this, null);        }} public class Mytestd {MYTESTC TESTC = new MYTESTC ();        Public event EventHandler ontestdevent = null;                private void Send_ontestdevent (object sender, EventArgs e) {if (ontestdevent! = null) {            Ontestdevent (sender, E);        }} public Mytestd () {testc.ontestcevent + = new EventHandler (testc_ontestevent);        } void Testc_ontestevent (object sender, EventArgs e) {send_ontestdevent (sender, E);        }} public class Myteste {mytestd TestD = new MYTESTD ();   Public Myteste ()     {this.testD.OnTestDEvent + = new EventHandler (testd_ontestdevent); } void Testd_ontestdevent (object sender, EventArgs e) {MYTESTC TESTC = sender as mytestc;//via MYT Estd the object forward if (TESTC! = null) {//code}}}

(2) Base keyword:
Generally used, subclasses access the parent class.
One is that when overriding a parent class method,

    public class ParentClass    {public        virtual void MethodA ()        {            Console.WriteLine (@ "Method of base class");        }    }    public class Childclass:parentclass    {public        override void MethodA ()        {            base. MethodA ();            Console.WriteLine ("derived class method");        }    }

Another, a subclass calls the parent class constructor,

    public class ParentClass    {public        ParentClass (string flage)        {            Console.WriteLine (@ "base class constructor");        } Public        virtual void MethodA ()        {            Console.WriteLine (@ "base class method");}    }    public class Childclass:parentclass    {public        ChildClass (string flage)            : Base (flage)        {        } Public        override void MethodA ()        {            base. MethodA ();            Console.WriteLine ("derived class method");        }    }

 3. What is reflection? How is reflection implemented? What are the pros and cons of reflection? When to use reflection?
http://blog.csdn.net/yysyangyangyangshan/article/details/7028589

The above is the basic knowledge of C #: C # class and Structure (2) of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.