13. C # division type and static class (chapter 7, Chapter 2-7.1 ),
If you don't find anything big, you will lose it. If you don't remember anything small, you will forget it. Today, I want to write the branch type and static tool class in C #. These two concepts may have been used in our daily use, maybe you think this is not something you should have. Let's review it.
The Division type. In my personal understanding, it refers to the description of the same thing in two different spaces. The Compiler combines the code written in different files into a complete description. We cannot write half of the code of a Member in one file, but put the other half of the Code in another file. Each Independent Member must be in the complete position of the file. If there is a Map class, the member attributes are placed in Map0.cs, and the member methods are placed in Map1.cs. Note that the partial keyword is used.
1 //Map0.cs 2 public partial class Map 3 { 4 public long Width { get; set; } 5 public long Height { get; set; } 6 } 7 //Map1.cs 8 public partial class Map 9 {10 public long GetSize()11 {12 return Width * Height;13 }14 }
Another common usage is to compile different implementations in different files when a class needs to implement an interface to inherit a class at the same time. There is a new Canvas class. The Code is as follows:
1 //Canvas0.cs 2 public partial class Canvas : IComparable 3 { 4 public int CompareTo(object obj) 5 { 6 return this.Price.CompareTo(((Canvas)obj).Price); 7 } 8 } 9 //Canvas1.cs10 public partial class Canvas : Map11 {12 public double Price { get; set; }13 }
From the above we can see that Canvas0.cs implements the IComparable interface, while Canvas1.cs inherits the Map class. Different files have different functions. But in our daily use, we really don't need to use the division type without having to use the division type. Otherwise, if there are too many files, we can't figure it out. We should leave it alone. The role of the Division type is often that when we compile our own classes, the background helps us to generate a class using partial to distinguish the ownership of code files.
Bytes ------------------------------------------------------------------------------------------------------
For static tools, all programs have their own or existing tools on the Internet, such as string operations and database operations. Some features of the tool class are described as follows:
Prevent class derivation. Use the sealed keyword to add a private constructor to prevent instance creation. The static keyword is used for static methods. This is the method for creating a tool class in C #1. See the code below, how to implement it in C # Later versions.
1 public static class StringHelper2 {3 public static int GetLen(string str)4 {5 return str.Length;6 }7 }
C # The Compiler knows that a class is a static class, so no default constructor is generated. Next, let's copy the textbook and list the many constraints that the compiler executes on the class definition:
Please make an axe.