C # class, static class,
C # is a computer language of the object-oriented type. class is used to embody the concept of object-oriented.
Classification)
In the real world, we will encounter many classified things. For example, animals, automobiles, and furniture. These things are called objects in computer languages. Taking cars as an example, the most basic accessories and functions of a car include tires, steering wheel, engine, and driving ability. In the programming world, we create a car object process called modeling. the modeling process encapsulates tires, steering wheels, engines, and driving capabilities into the model. Encapsulation is one of the major characteristics of object-oriented. We store tires, steering wheels, and engines in the form of fields (fields) in the model and will be able to drive in the form of methods in the model. The following code snippet creates a car model or a car class.
class Automobile{ private string _steeringWheel; private string _tire; private string _engine; public void Run() { }}
When a class is created, this class sets a boundary to isolate the inside and outside of the class. So how do external fields and methods Inside the category? Public modifier, which allows external class access. Private modifier, which only allows internal class access.
_ SteeringWheel, _ tire, _ engine. These fields are private and cannot be accessed outside the class. Run () method, public modifier, externally accessible. We encapsulate some attributes and methods in the Automobile class. Encapsulation has two meanings:
1. combine attributes and methods. In other words, it is classification. Classify auto parts and functions from other things.
2. Control the access to properties and methods. You can specify an access modifier to control whether or not the internal fields and methods of the external class are allowed.
Static methods and data:
Static methods, unlike common methods, can only be called through classes. A common method can only be called by instantiating an object. For example:
Class Test {pulbic static DoWrok () {DoWork2 (); // No access is allowed even if DoWork2 and the method are in the same class. // The static method can only access the static method. The static field} public void DoWork2 () {}} Test. doWork (); // you can call Test successfully. doWork (2); // compilation failed Test object = new Test (); // The Test object is instantiated. doWork2 (); // call a method by instantiating an object
Generally, static methods are common methods. They are frequently used in programs and do not need to instantiate objects every call. There are static methods and static classes. Pay attention to the following points about static classes:
1. Only static members are allowed in static methods. In other words, fields in static classes must be static.
2. Use of static classes and encapsulate some common tools.
For static fields and constant fields, note that constant fields cannot be marked as static fields, but they are static.
Anonymous Class ):
As the name suggests, an anonymous class is a class without a name. We use the var keyword to define its type. There are some restrictions on anonymous classes:
1. The Anonymous class can only contain public fields
2. The Anonymous class cannot contain methods.
3. Anonymous fields cannot be defined as static fields.
4. Anonymous fields must be initialized.
var Anonymous =new {Name="Diana",Age=46};