Recall the important. NET knowledge that we once remembered, and the important. net knowledge.

Source: Internet
Author: User

Recall the important. NET knowledge that we once remembered, and the important. net knowledge.

As the title says, what are the. NET knowledge points?

Next let me take you through these knowledge points!

1. Interface

2. Indexer

3. Nature of FOREACH

4. Anonymous internal class

5. overload of Operators

 

I.What is an interface? What is the role of the interface? If it's not clear or something else, you can browse it carefully and completely several times! I think it will be of great help to you in the future.

Resolution: an interface is a protocol between components. It describes the external services provided by components. Technically speaking, interfaces are a set of data structures that contain functional methods. Through this set of data structures, the customer code can call the functions of component objects. Interfaces can be inherited from the parent interface. The inheritance of interfaces is descriptive inheritance first, rather than implement inheritance. Its implementation needs to be implemented through classes or structures. Secondly, interface inheritance can be multi-inheritance.

 

Purpose:

01. An interface exists to constrain the format (parameter and return value type) of the method.

 

02. The interface can implement multi-inheritance to make up for the defect of single inheritance.

 

03. The interface can be considered as a special abstract class. We can see the source code through decompilation.

 

04. access modifier is not required for methods in the interface, because CLR will automatically add and there cannot be a method body

 

05. If a class implements an interface, all methods of the interface must be implemented.

 

06. Use the interface with caution to prevent interface contamination!

 

07. An interface represents only one capability. The class implementing this interface has no inheritance relationship with the interface.

 

08. interfaces are implemented and classes are inherited.

 

09. In fact, many times, it seems that interfaces are not needed, because interfaces are a method convention,

It indicates that you must have some methods for this class, but you can also use these methods without writing interfaces,

 

Interface variables can be used for unified calls to achieve Polymorphism

 

1.Create an interface and let a class implement this interface

 

Public class Person: IFly {public string Say (string name) {return "Person" ;}} public class Plane: IFly {public string Say (string name) {Console. writeLine ("flying in the sky"); return "Flying ";}
}

 

 

 

 

2.Define an array of the IFly type of an interface to instantiate classes that implement the IFly Interface

IFly [] ifly = {new Plane (), new Person ()}; foreach (IFly item in ifly) {string name = item. say ("Everything that can fly"); Console. writeLine (name );}

 

3.The interface can also inherit the interface

 public interface IPlay:IFly    {         void Play();    }

 

2. Let's talk about the index tool that makes coding more convenient!

1.The essence of attributes is method, and the essence of Indexer is attribute. (The indexer may be better understood)

2. Indexer

Class Members in 01. C # can be of any type, including arrays and collections. When a class contains arrays and set members, the indexer greatly simplifies access to arrays or set members.

 

02. The way to define the index is similar to the way to define attributes. The general form is as follows:

 

[Modifier] data type this [index type index]

 

{

 

Get {// code for obtaining attributes}

 

Set {// set the Property Code}

 

}

 

03. The essence of the indexer is also a class (see the source code)

 

04. Code for creating an index

Public class Student {private string [] name = new string [2]; public string this [int index] {get {return name [index];} set {name [index] = value ;}} static void Main (string [] args) {# region 02. indexer Student stu = new Student (); stu [0] = "Dong zewen"; stu [1] = "Zhang yiming"; Console. writeLine (stu [0] + "\ n" + stu [1]); # region

 

 

3. Foreach principles that you should be particularly interested in:

Essence: implements an IEnumerable interface,

01. Why can I use foreach to traverse arrays and collections?

Resolution: Because arrays and collections both implement the IEnumerable interface, there is only one method in this interface, GetEnumerator ()

02. An array is a data structure that contains several variables of the same type. The array uses the type declaration: type [] arrayName;

03. The Array type is a reference type derived from the abstract base type Array. This type implements IEnumerable, so you can use foreach Iteration for all arrays in C.

04. Interview Questions:

001. C # the type of the ______ interface or the ______ method must be implemented for objects that can be accessed with foreach traversal.

Resolution: IEnumerable, GetEnumerator ()

 

05.MyConnection class that implements the IEnumerable Interface

 

 

 public class MyConnection:IEnumerable    {        ArrayList list = new ArrayList();        public void AddList(object o)        {            list.Add(o);        }        public IEnumerator GetEnumerator()        {            return new MyIenumerator(list);        }    }

 

 

06.Class for implementing the IEnumerator interface MyIEnumerator

  public class MyIenumerator:IEnumerator    {        public ArrayList list = new ArrayList();        private int i = -1;        public MyIenumerator(ArrayList mylist)        {            list = mylist;        }        public object Current        {            get { return list[i]; }        }        public bool MoveNext()        {            bool flag = false;            if (i<list.Count-1)            {                i++;                flag = true;            }            return flag;        }        public void Reset()        {            throw new NotImplementedException();        }    }

 

 

07. instantiate MyIEnumertor and use FOREACH to loop

MyConnection mycon = new MyConnection (); mycon. addList ("Dong zewen"); mycon. addList ("Zhang yiming"); foreach (object item in mycon) {Console. writeLine (item );}

 

 

Iv. Anonymous internal class

 

02. Anonymous classes will also be compiled into a named class by CLR at the underlying layer.

 

03. The anonymous type provides a convenient method to encapsulate a set of read-only attributes into a single object without explicitly defining a type. The type name is generated by the compiler and cannot be used at the source code level. The type of each attribute is inferred by the compiler.

 

You can use the new operator and object initial value to create an anonymous type.

 

 

# Region 04. anonymous internal class var num = 1; var names = "1"; var falg = false; var stu = new {name = "Dong zewen", age = 12 }; if (num = 1 & names. equals ("1") {num ++; names + = "10"; falg = true;} Console. writeLine (num + "\ t" + names + "\ t" + falg); Console. writeLine (stu. name + "\ t" + stu. age); # endregion

 

 

 

 

 

5. Operator Overloading

 

01.C # allows users to define types and use the operator keyword to define static member functions to overload operators.

 

02. Override Operator Methods class

Public class Personers {public string PName {get; set;} public int PAge {get; set;} // rewrite the addition operation public static int operator + (Personers p1, Personers p2) {return p1.PAge + p2.PAge;} // rewrite the subtraction operation public static int operator-(Personers p1, Personers p2) {return p1.PAge-p2.PAge ;} // override the multiplication operation public static int operator * (Personers p1, Personers p2) {return p1.PAge * p2.PAge;} // override the division Operation public static double operator/(Personers p1, personers p2) {return p1.PAge/p2.PAge;} // rewrite the constant arithmetic operation (which must be written at the same time as the unequal method) public static bool operator = (Personers p1, Personers p2) {return p1.PAge = p2.PAge;} // rewrite the unequal operation (it must be written at the same time as the constant method)
 public static bool operator !=(Personers p1, Personers p2) { return p1.PAge != p2.PAge; } }

 

 

 

 

03. instantiation

 

 Personers p1 = new Personers();            Personers p2 = new Personers();            p1.PAge = 25;            p2.PAge = 12;            Console.WriteLine(p1 + p2);            Console.WriteLine(p1 - p2);            Console.WriteLine(p1 * p2);            Console.WriteLine(p1 / p2);            Console.WriteLine(p1 == p2);

 

 

 

This is the current learning content. If you think it is helpful to you, please wait for my updates! If you have insufficient knowledge, please give me more suggestions!

 

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.