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

Source: Internet
Author: User
1. What are the functional characteristics of static and static members? Implementation code?
Static and static members are classes or members that are defined with the static keyword, and the members of the static class must be static members, or they will get an error. One of the great features of static classes and members is that they are unique. If it is a static class, then it is not instantiated, and loaded in memory only one, if it is a static variable, the method, the class can be instantiated, no matter how many times, static variable or method is always only one.
As follows:
(1), Static members

    public class Statticmember    {public        static string TestA = String. Empty;    }        Class program    {        static void Main (string[] args)        {            //staticconstruct strc = new Staticconstruct ();            Staticconstruct strcvalue = new Staticconstruct (string. Empty);            Statticmember sMember1 = new Statticmember ();            Statticmember.testa = @ "static member";            Console.WriteLine (Statticmember.testa);            Statticmember sMember2 = new Statticmember ();            Console.WriteLine (Statticmember.testa);            Console.ReadLine ();        }    }

Results:

Static member Features:
A, must be referenced by the class name, can not be referenced by the object of the class;
b, no matter how many times the class is instantiated, only the same area in memory;
C, static methods if you reference variables or methods outside the method, they must also be static, such as

    public class Statticmember    {public        static string TestA = String. Empty;        Public  String testb = string. Empty;        public static void Method ()        {            TestA = @ "my";//correct            //TESTB = @ "my";//Error        }    }

(2), Static class

  public static class Staticclass    {public        static string TestA = String. Empty;        public static void Staticmethod ()        {            Console.WriteLine (@ "static method");        }    }    Class program    {        static void Main (string[] args)        {            Staticclass.testa = @ "Static class";            Console.WriteLine (Staticclass.testa);            Staticclass.staticmethod ();            Console.ReadLine ();        }    }

Results:

Static class Features:
A, the member must also be static;
B, can not be instantiated, referring to internal members directly with the class name;
C, is sealed class, (note: Sealed class refers to this class can not be used as a base class, cannot be an abstract class, that is, cannot be derived.) )
D, cannot contain constructors.
Static classes and member usage situations, static classes cannot be flooded, because once it is loaded, there is an area in memory that is there, whether you use it or not. Takes up memory. This can be used in the following situations:
A, global variables, a variable used throughout the project, and the value is not easily changed, even if the change of all modules must be responded to.
B, do not manipulate instance data, methods that are not associated with a particular class in your code, such as some methods in the math class.
2, sealing class function characteristics? Implementation code? Why use sealed classes?
A sealed class is a class that is decorated with the sealed keyword, which is designed to prevent derivation, that is, the type cannot be inherited.
Characteristics:
cannot be used as a base class, cannot be abstracted, and the invocation of a sealed class is relatively fast.

    public sealed class Sealedclass    {        public  string TestA = string. Empty;    }

3. What is abstract class? Functional characteristics? Implementation code? What is the difference between an interface and an abstract class?
Abstract classes are classes that are decorated with the abstract keyword. The role is to derive multiple classes, sharing common methods and properties of the base class.

   Public abstract class AbstractClass    {public        abstract void Commonmethod ();    }    public class Childclass1:abstractclass    {public        override void Commonmethod ()        {            Console.WriteLine (@ " Implement common Method 1 ");        }    }    public class Childclass2:abstractclass    {public        override void Commonmethod ()        {            Console.WriteLine (@ "Implement common Method 2");        }    }    Class program    {        static void Main (string[] args)        {            ChildClass1 chc1 = new ChildClass1 ();            Chc1.commonmethod ();            ChildClass2 chc2 = new ChildClass2 ();            Chc2.commonmethod ();            Console.ReadLine ();        }    }

Results:


The difference between an abstract class and an interface:
A, class is an abstraction of an object, which can be understood as a class as an object, an abstract class called an abstract class. While the interface is just a specification or rule of behavior, Microsoft's custom interface is always followed by a able field, proving that it is stating a class of classes "I can do ...". Abstract classes are more defined in a series of tightly related classes, and interfaces are mostly loosely-connected but implement a function of a class;
b, the interface basically does not have any specific characteristics of inheritance, it only promises to be able to invoke the method;
C, a class can implement several interfaces at a time, but can only extend a parent class;
D, the interface can be used to support callbacks, and inheritance does not have this feature;
E, abstract class can not be sealed;
F, the specific method of the implementation of the abstract class default is virtual, but the implementation of the interface of the class interface method is not virtual, of course, can also be declared as virtual;
g, (interface) is similar to a non-abstract class, and an abstract class must also provide its own implementation for all members of the interface listed in the base class list for that class. However, an abstract class is allowed to map an interface method to an abstract method;
H, abstract class implements a principle in oop, separating mutable from immutable. Abstract classes and interfaces are defined as immutable, and the variable is given to subclasses to achieve;
I, the good interface definition should be has the specificity function, but is not multifunctional, otherwise causes the interface pollution. If a class just realizes the function of this interface, and has to implement other methods in the interface, it is called interface pollution;
J, if an abstract class implements an interface, you can map the methods in an interface to an abstract class as an abstract method without having to implement the method in the subclass of the abstract class.

Above is the above is the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)! For more information, 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.