C # Fundamentals collation: C # Classes and structs (4)

Source: Internet
Author: User
1. What is an interface? Functional characteristics? Implementation code?
An interface is a set of specifications that is defined using the interface keyword, consisting of a combination of members of a class, describing some functionality. As can be seen in C #, some of the system's interfaces are named: IComparable (Type comparison method), ICloneable (Support clone), IDisposable (freeing resources) and so on, I represents the interface, and able reflects the characteristics of the interface: "Can ... "To show what this set of specifications could do.
(1), interface implementation

   public interface Iprintable {void printstring ();        void Printint ();    void Printbool ();        } public interface Icomputeable {void handlerstring ();        void Handlerint ();    void Handlerbool ();        } public class myinplementinterface:iprintable, icomputeable {//implicitly implement public void printstring ()        {Console.WriteLine (@ "1");        } public void Printint () {Console.WriteLine (1);        } public void Printbool () {Console.WriteLine (true);        } public void Handlerstring () {Console.WriteLine (@ "1" + "1");        } public void Handlerint () {Console.WriteLine (1 + 1);        public void Handlerbool () {Console.WriteLine (true | | false);        }//Display implementation//void icomputeable.handlerstring ()//{//throw new NotImplementedException (); }//void IComputeable.handlerint ()//{//throw new NotImplementedException ();        }//void Icomputeable.handlerbool ()//{//throw new NotImplementedException (); }} class Program {static void Main (string[] args) {Myinplementinterface imple = n            EW Myinplementinterface (); Imple.            Printstring (); Imple.            Printint (); Imple.            Printbool (); Imple.            Handlerstring (); Imple.            Handlerint (); Imple.            Handlerbool ();        Console.ReadLine (); }    }

Results:

(2) Implement a dedicated interface, which is a C # already defined interface
Cases:

    public class implementsysinterface:icomparable    {public        int CompareTo (object obj)        {            // You can implement your own comparison method as needed            return 0;        }        private void Usingmenthod ()        {            //error, because Noidisposeableclass does not implement the IDisposable interface, the using//using is not supported            ( Noidisposeableclass my = new Noidisposeableclass ())            //{            //}            //After implementing the IDisposable interface, you can use the using using            (Idisposeableclass my = new Idisposeableclass ())            {            }}} public class    idisposeableclass: noidisposeableclass {} IDisposable    {        #region IDisposable member public        void Dispose ()        {                    }        #endregion    }

The interface has the following characteristics:
A, the interface is similar to the abstract base class, cannot instantiate the interface directly, the method in the interface is an abstract method, any non-abstract type that implements an interface must implement all members of the interface:
B, when the member of the interface is explicitly implemented, the implemented members cannot be accessed through the class instance, only through the interface instance.
For example:

   public class myinplementinterface2:icomputeable    {        void icomputeable.handlerstring ()        {            Console.WriteLine (@ "1" + "1");        }        void Icomputeable.handlerint ()        {            Console.WriteLine (true | | false);        }        void Icomputeable.handlerbool ()        {            Console.WriteLine (true | | false);        }    }    Class program    {        static void Main (string[] args)        {            icomputeable imple2 = new MyInplementInterface2 ( );            Imple2. Handlerstring ();            Console.ReadLine ();        }    }

C, when the members of the interface are implicitly implemented, the members of the implementation can be accessed through the class instance or through the interface instance, but the members must be public.
D, interfaces cannot contain constants, fields, operators, instance constructors, destructors, or types, and cannot contain static members.
E, interface members are automatically exposed and cannot contain any access modifiers.
F, the interface itself can inherit from multiple interfaces, classes and structs can inherit multiple interfaces, but interfaces cannot inherit classes.

2. What is a generic type? What are the benefits of generics?
Generics are the introduction of the concept of type parameters to. NET, which enables you to manipulate multiple data types on the same code by using parameterized types. is a reference type and is a heap object.
In fact, the beginning of the study of generics, is in the learning of Java, at that time did not understand, I have always felt that the generic is purely superfluous, with object as can be done. For example, if, for example, someone has to print out a value of type, then object implements:

    public class Test    {        private object model;        public Object Model        {            get            {                return Model;            }            Set            {                model = value;            }        }        Public Test (object model)        {this            . model = model;        }        public void ShowRecord ()        {            Console.WriteLine (model);        }    }   Class program    {        static void Main (string[] args)        {            int recordi = 2;            bool Recordb = true;            Test testi = new test (Recordi);            Testi.showrecord ();            Test TESTB = new test (RECORDB);            Testb.showrecord ();            Console.ReadLine ();        }    }

But when you learn more, you will find that there are certain problems. The first is the boxing problem, where int is a value type, and when assigned to the object type, a boxing operation is completed. What is boxing? is to copy the Recordi value into the space allocated by the new object. Wasted time and performance. So generics are still useful, so use generics to achieve:

  public class testgeneric<t>    {        private T model;        Public T Model        {            get            {                return Model;            }            Set            {                model = value;            }        }        Public testgeneric (T model)        {this            . model = model;        }        public void ShowRecord ()        {            Console.WriteLine (model);        }    }    Class program    {        static void Main (string[] args)        {            int recordi = 2;            bool Recordb = true;            testgeneric<int> testgi = new testgeneric<int> (Recordi);            Testgi.showrecord ();            testgeneric<bool> TESTGB = new testgeneric<bool> (RECORDB);            Testgb.showrecord ();            Console.ReadLine ();        }    }

Thus, when testgeneric<int> testgi = new Testgeneric<int> (Recordi), T is int, and it is not necessary to pack.
Of course generics are not just to solve the boxing problem, but the features are as follows:
A, avoid packing and unpacking, improve the performance;
b, improve the reusability of the Code;
C, type-safe, because in the compile time will be detected;
D, you can create your own generic interfaces, generic classes, generic methods, generic events, and generic delegates.
The above is the basic knowledge of C #: C # class and Structure (4) 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.