C # basic series-Class 2 (static class and Extension Method)

Source: Internet
Author: User

In fact, there is no association between the two articles. This time we will learn static classes. We will first create two projects-assemblylibrary (Class Library) and consoltest (console)

  

A static class is a special class with four features:

Contains only static members. It cannot be instantiated. It cannot be inherited and cannot contain instance constructors.

Assemblylibrary:

Public static class staticclass {// string a; error: contains only static members static string a; static staticclass () {A = "1" ;}// error: no instance constructor // public staticclass () // {//} public class SS: butongclass // error: Unable to inherit staticclass {public SS (): Base (1) {}} public class butongclass {public readonly static int I = 0; static butongclass () {I = 1;} public bool equle; Public butongclass (int) {if (a = I) equle = true ;}}

Consoltest

Static void main (string [] ARGs) {butongclass butong = new butongclass (1); // staticclass SC = new staticclass (); error: cannot be instantiated console. writeline (butong. equle); console. read ();}

I commented out all the errors. The error point is also attached.

Running result: True.

  

If you want to assign values to static Read-Only variables

Public readonly static int I = 0; in this way, values can be assigned, or values can be assigned to static constructors.

The variables in the static constructor must be static members and can be executed only once. With this feature, we can achieve some unexpected results, such as the singleton mode.

Traditional code:

View code

 public class SingeClass    {        private static SingeClass _instance;        private static object o = new object();        public static SingeClass GetInstance()        {            if (_instance == null)            {                lock (o)                {                    if (_instance == null)                        _instance = new SingeClass();                }            }            return _instance;        }    }

We can use the characteristics of static constructor to implement a singleton:

    public class SingeClass    {        private static SingeClass _instance;        static SingeClass()        {            _instance = new SingeClass();        }
    private SingeClass(){}

public static SingeClass GetInstance() { return _instance; } }

Last night I wrote not strictly enough. Now I set the default constructor to private, so that the external cannot be instantiated. Thank you for your attention,

Static constructor is rarely used. If there are multiple static constructor called by. Net during its call, it is unknown to call it first. Static class, the most commonly used extension method. Above framework3.5,

The namespace system. LINQ uses a large number of extension methods.

 

Extension Method, which must be a static class. The extension method must also be a static method.

This is an example of an extension method in system. LINQ: we can see a static Extension Method Name (this extension object, XXXXX)

Let's take a simple example.

    class Program    {        static void Main(string[] args)        {            string str = "1"; ;            Console.WriteLine(str.ToInt());            Console.Read();        }    }    public static class StringEx    {        public static int ToInt(this string source)        {            int result;            if (int.TryParse(source, out result))                return result;            return 0;        }    }

It's easy ~~~ I still have to like this extension method. You can directly use the smart tip to get the method name after a few clicks on the code, provided that you have applied the extension method you have written.

 

Stick to Monday to Thursday. After I finish the basics, I will review myself.

 

 

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.