C # Getting Started basic three

Source: Internet
Author: User
Tags abstract definition

Encapsulation: simplifies the user interface and hides implementation details. Get{return property value;} set{Property value = ' value ';}

Inheritance: Subclasses inherit all non-private members of the parent class. Inheritance has transitive, single-root sex. Implicit inheritance: implemented in quotation marks (:). Show inheritance: the keyword (: base) implementation.

Show the difference between inheritance and implicit inheritance: simply to say that if there are 2 or more 2 interfaces in the inherited interface with the same name of the property/method, and they are different functions, you should display the inheritance interface in turn arbitrary (generally implicit, because can be lazy) if you hesitate to use which And you have to make sure you don't. Directly using explicit

Override of method: Overrides must occur on the basis of inheritance. The Available keyword virtual is set to virtual method, which is overridden with override. or rewrite it with the keyword new. The virtual keyword uses the syntax: [access modifier]virtual[return type] Method name (parameter list) override to override a virtual method, an already overridden method, an abstract method

Abstract classes and Abstract methods: Abstract Classes: Classes that cannot be instantiated are called abstract classes, and abstract classes are the basis of derived classes. Abstract classes can only be inherited and implemented by ordinary classes. Keyword abstract class cannot be instantiated, can be inherited by ordinary classes, and implements the definition syntax: Abstract class< class name >

Abstract methods: Abstract methods have no method body and can only be defined in abstract classes.

Abstract classes may not write abstract methods, but abstract methods must be placed in abstract classes.

Interface: Definition syntax: [access modifier]interface< Interface name > Keyword: interface defines interface name usually starts with "I", Example: Iname interface body cannot write method with method body; cannot write access modifier; Cannot write property.

The difference between an interface and an abstract class: An abstract class interface

Defined with interface by the abstract definition

Only one class can be inherited to implement multiple interfaces

A class that does not abstract derived classes must implement an abstract method to implement an interface must implement a different point for all members

Need override to implement abstract method directly

cannot be instantiated the same point contains the method that is implemented by the derived class (subclass) must implement the method that is not implemented

===============================================================================================

Indexer: Keyword ==this "C # can handle an object as if it were an array by providing an indexer. In particular, attributes, each element is present in a get or set method. Role: Indexers can access class members as if they were accessed by an array of users.

Syntax: [access modifier] Data type this[data type identifier]

{get{} set{}}

Example: namespace Syq {

Class Program

{static void Main (string[] args)

{Clas CLS = new Clas (3);

Student stu = null; Create three student information and load student information into the class

for (int i = 0; i < 3; i++) {

Stu = new Student ("Blue sky" + (i + 1));

Cls[i] = stu; }

Retrieve by index and display the result

Console.WriteLine (Cls[2]. Name);

Retrieve by name and display the results

Console.WriteLine (cls["Blue Sky 1"].         Name); }     }

Class CLASS3 {}///<summary>//Student information///</summary> public class Student

   {        private string _name;          Public Student (string name)         {             this._name = name;        }         public string Name          {            get { return this._name; }        }    }    //<summary>     ///class, the collection of students    //</summary>     public class Clas   & nbsp {       //arrays for student information         private Student[] _students;        //To create a class you must specify the size of the class        public Clas (int count)         {             this._students = new Student[count];        }        //Pass index is used to retrieve an array of photos          public Student This[int index]         {             get {                     if (Index < 0 | | Index >= THIS._ Students. Length)//Verify index range                      {                         Console.WriteLine ("Invalid Index");                         return null;                     }                      return _students[index];//for valid indexes, returns the requested student information                 }             set {                      if (Index < 0 | |, index >= this._students. Length)//Verify index range                      {                         Console.WriteLine ("Invalid Index");                          return;                     }                     //For valid indexes, like array loading new student information                      This._students[index] = value;                }         }        //The index of the image retrieved by student name defined here is read-only          public Student this[string name]         {             Get              {&Nbsp;              //Iterate through all student information in the array                  foreach (Student s in this._ Students)                 {                     if (s.name = = name)//Compare the student's name to the indexer parameter                          return s;                }                  Console.WriteLine ("Not Found");                 return null;            }

}     } }

Delegate: Keyword ==delegate "delegate contains a reference to a method instead of a method name. Delegates can be used to dynamically set the method to invoke at run time. A delegate can be understood as a pointer to a method, which is type-safe. It has two major characteristics: object-oriented, type-safe and reliable.

Delegate to execute a method, the return type and parameter list must be consistent with the method. Syntax: [access modifier]delegate return type delegate name (parameter list);

Example: namespace Weituo {    class program     {        static void Main (string[] args)         {             Pengge pp = new Pengge ();             Pp.chi ();        }        //define delegates. If the delegate executes the method, the return type must be the same as the method         public delegate string PGG (int money);        //Delegate         public class Pengge         {            public void Chi ()             {                 Zhaoshuai mm = new Zhaoshuai ();   //Instantiation of Delegates                 PGG cc = new PGG (MM.MAI2);//Invoke Delegate                  Console.WriteLine (CC (2));            }        }        /Performers         public class Zhaoshuai         {            public string Mai ()             {                 return "sesame cake";            }              public string Mai2 (int money) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;     {                 return "buy sesame cake again?!!!! ";            }        }    }} "

Event: Keyword ==event "event is a special kind of delegate.

Syntax: [access modifier]event delegate name event name;

Example: namespace Shijian {

Class Program {

     static void Main (string[] args)         {             Pengge PGG = new Pengge ();             Pgg.chi ();        }        /define Delegates          public delegate void Dlgt ();         public class Pengge         {            //define Events              public event Dlgt MyEvent;             public void Chi ()              {            Zhaoshuai ZS = new Zhaoshuai ();            //Subscribe to Events             MyEvent + = new Dlgt (Zs.maifan);             MyEvent + = new Dlgt (Zs.toudu);             MyEvent + = new Dlgt (SI);            //Triggering events              MyEvent ();            }              public void Si ()             {                 Console.WriteLine (" It turns out to be ");            }        }         public Class ZhaoShuai         {             public void Maifan ()             {                 Console.WriteLine ("Buy another meal?! ");            }              public void Toudu ()             {                 Console.WriteLine (" Peng Brother Mighty! Poison you! ");            }        }    }} "

The difference between an event and a delegate: An event is a narrowly defined delegate, that is, an event is a dedicated delegate for an event-driven model.

In layman's words, a delegate can invoke a delegate directly in the client code to fire the function that the delegate points to, and the event cannot be triggered by the service code itself only

That is to say that in your code you can not only arrange who is its calling function, but also call it directly, and the event cannot be called directly, but only through certain actions.

C # Getting Started basic three

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.