C # Basic Primer three categories
- Class is declared with the class keyword, preceded by an access modifier, public class car{}
- Access modifiers: The master can be used to modify classes and class members, etc., to control their visibility
- Modifier keywords are: public,internal,protected,private
- Use the four access modifiers above to specify the following five levels of accessibility:
- Public: Unrestricted access;
- Protected: Access is limited to types that contain classes or derive from contained classes;
- Internal: Access is limited to the current assembly;
- Protected internal: Access is restricted to the current assembly or from a category that contains derived types;
- Private: Access is limited to the containing type.
Object
- Objects are specific, objects are also called instances, can be stored in named variables, or stored in arrays or collections.
- The object of the class is created using the new operator.
Static class
- A class declared with the static modifier is called a static class, and a static class is basically the same as a normal class, but there is a difference between a static class that cannot be instantiated , that is, a variable of a static class type cannot be created with the New keyword. Because there is no instance variable, you use the class name itself to access the members of the static class.
- Members that are decorated with static are called static members, and static methods and properties cannot contain non-static fields and methods.
- Static fields have two common uses: one is to record the number of objects that have been instantiated, and the other is to store values that must be shared across all instances.
- A non-static member is manipulated by an object, while a static member is operated by a class.
Class member Field
- A field is any type of variable declared directly in a class or struct, and a field is a "member" of its containing type. Declare these fields in a class block by making a field's access level and then specifying the type of field, and then specifying the name of the field.
public string name="zxh";
Property
- Properties are members: it provides a flexible mechanism to read, write, or calculate values for a private field, use attributes like public data members, but in fact they are special methods called accessors, which makes it easy to access data, and also helps to improve the security and flexibility of the method.
public class Car { protected string brand="BMW"; public string Brand { get { return brand;//get访问器,用来读取属性值 } set { brand = value;//set访问器,用来给属性赋值 } } }
-
Note: Properties that do not have accessors are treated as read-only properties, properties that do not have a get accessor are treated as write-only properties, and properties that have both accessors are read-write properties. For example (Figure 7), the set comment is dropped, and the following is an error for brand assignment, at which time the property is read-only. Similarly, if get is commented out, the property is unreadable and cannot be printed.
-
Attributes can be marked as public, private, protected, internal, or protected internal, and the get and set accessors of the same property may have different access modifiers, for example, The get may be public to allow read-only access from outside the type, and the set may be private. (You can use the accessor modifier only if the property has both get and set accessors, and only allow modifiers for one of the accessors)
Method
- The method is actually a function, but the function inside the class is often called a method
- Method is a code snippet with specific function, the use of the method can save the writing of repeated code, greatly simplify the program and improve the development efficiency.
- Methods can be divided into library methods, such as Console.Write (), and custom methods.
- Method definition: Access modifier return value type (Void/int/char ...) function name (formal argument list) {statement; return value;}
- Defines a Run method
public class Car { public string brand="BMW"; public string Brand { get { return brand; } set{ brand = value;} } public void Run(int oil) { Console.WriteLine("车里有{0}升油", oil); return; } }
- The return value of the method is called the value of the method and can only be returned to the keynote function through the return statement, a method can have at most one return value, the method can use return to end the execution of the method even if there is no return value, and return to the keynote method, the statement after the return is no longer executed.
C # Basic Primer Three