Classes in C #
C # is an object-oriented language, and classes are a complex type of data in C #.
C # defines a basic format for a class:
Class name
{
Class member definitions
}
By default, if class does not display the specified access modifier before, the access modifier for the class is internal, which means that it is accessible only within the current project.
In addition to the modifiers in internal,c# there are modifiers such as private, protected, public, abstract, sealed, and a combination of these modifiers.
Modifier access Permissions
None or internal can only access classes in the same assembly
Public the same assembly or other assemblies that reference the assembly can be accessed
Abstract or internal abstract can only access the class in the same assembly, the class cannot be instantiated and can only be inherited
Public abstract The same assembly or other assemblies that reference the assembly can access the class, cannot be instantiated, and can only be inherited
Sealed or internal sealed can only access the class in the same assembly, the class cannot be inherited and can only be instantiated
Public sealed the same assembly, or other assemblies that reference the assembly, can access the class, which cannot be inherited and can only be instantiated.
Members of the class:
Modifier access Permissions
Public the same assembly or other assemblies that reference the assembly can be accessed
Private can only be accessed in the same class
Protected can only be accessed in the same class or in a derived class
Internal only the same assembly can be accessed
Protected internal the same assembly, which can be accessed in this class and in derived classes.
Field:
You can use the keyword readonly or const to define a field. ReadOnly: Indicates that the field is read-only (does not need to be initialized at the time of definition and can be initialized in the constructor) const: Indicates that the field is immutable (a compilation error occurs if the field is not initialized when it is defined).
You can also use the static keyword to declare a static field. The difference between a static field and an instance field is that a static field must be accessed through a class, whereas an instance field needs to be accessed through an object instance of the class.
Property:
A property is an extension to a field. Based on the basic idea of object-oriented, the field is best set to private because it prevents the client byte from modifying the field, thus guaranteeing the integrity of the internal members. In order to access the private fields of a class, the property mechanism is provided in C #
eg
public class Person
{
private string name;
public string Name
{
Get accessor
get{
return name;
}
Set accessor
set{
Value is an invisible parameter
name = value;
}
}
}
You can avoid calling methods by accessing fields through properties.
When a property contains only a get accessor, or if the set accessor is private, such a property is called a read-only property and is called a write-only property when the property contains only the set accessor, or if the get accessor is private.
Like static fields, properties can also be declared as static properties through the static keyword, and non-static members cannot be used in static properties
Method overloading
Method overloading means that multiple names can be defined in a class, but the type parameter order, the parameter type, the number of arguments, and the return type are not part of the method overload.
constructor function
constructor to create an instance object of the class
There are two types of constructors, static constructors and instance constructors.
1. Instance constructors: Used to create and initialize instances of a class
Constructors have the following characteristics:
1) Constructors can perform method overloading
2) If there is no definition, an empty constructor is generated by default
3) The function name of the constructor must be the same as the class name
4) No return value allowed
2. Static constructors (static constructors execute only once): used to initialize static members of a class, and the CLR automatically calls the static constructor before the first instance is created or any static members are referenced
Static constructors have the following characteristics:
1) Static constructors cannot use any access modifiers
2) Static constructors cannot have any parameters
3) The static constructor executes only once
4) cannot call static constructors directly
5) Programmers cannot directly control the timing of a static constructor call
Destructors:
Used to dispose of the managed and unmanaged resources used by the class instance before the class is destroyed. The destructor implicitly invokes the Finalize method of the base class object
try{}catch{} is the exception handling mechanism for C #,
The destructor needs to be noted:
1) destructors can no longer be defined in structs, only in classes
2) A class can have only one
3) cannot inherit or overload destructors
4) cannot display call destructor, automatically called by garbage collector
5) destructors have no modifiers and no parameters
Indexer: Easy access to array members in a class
When a class contains an array member, the use of the indexer greatly simplifies access to the array members in the class, defined like attributes, and Get,set accessors, as defined below:
[modifier] Data type This [indexed type index]
{
get{}
set{}
}
Structure:
A struct type and a class type are syntactically similar, and are data structures that can contain both members of a database and a method.
There is a case where a class two variable points to the same object.
A struct is a value type that directly contains its own data, and each structure contains its own piece of data. Modifying the data of one structure does not affect the data of other structures.
The struct has a default parameterless constructor
In inheritance, a method that is written in a derived class that is the same as the parent class function name, the parent class method hides the warning, and the elimination warning uses the new keyword before the derived class method, how do you call the parent class with the same name method? By strong turn, the derived class is strongly turned into a parent class.
Comparison of structures and classes
Data type Structure class
Whether the no reference type must be instantiated with the new operator
Whether the parameterless constructor can be declared no Yes
Data members can be declared as const or static at the same time as declared, and data members cannot
Directly derived from what type System.ValueType have
Whether there is a destructor no
Can derive from class no OK
Can the interface be implemented to To
When instantiating a heap in a stack or heap, save a reference in a stack
Whether a variable of this type can be assigned a value of NULL no
Can I define a private parameterless constructor
Whether there is always a default parameterless constructor
The difference between classes and struct bodies
1: Class definition using class, struct using structs
2: The declared field cannot be initialized in the struct, and the class can
3: In a struct, whether or not we have a constructor defined, an implicit constructor always exists
4: Structs cannot explicitly define parameterless constructors
5: Struct constructors must assign values to all fields
6: Create struct object can not use new, class must use
7: Structs cannot inherit structs or classes, but interfaces can be implemented, classes can inherit classes but cannot inherit structs, and interfaces can be implemented
8: Class is a reference type, struct is a value type
9: Structs cannot define destructors, classes can
10: You cannot use abstract or sealed to modify a struct, whereas a class can.
C # is an object-oriented language, and all object-oriented languages have three basic features, which are:
Encapsulation: to encapsulate objective things into classes, and to hide the inner implementation of the class to guarantee the number
The integrity of the data.
Inheritance: You can reuse the code of a parent class through inheritance.
Polymorphism: A capability that allows child objects to be assigned to a parent object.
Sealing class:
Sealed classes are classes that cannot be inherited, and forced inheritance generates compilation errors lc# uses the SEALED keyword to define the sealed class.
Initialization Order of subclasses:
1) Initialize the instance field of the class
2) Call the constructor of the base class, and if there is no base class, call the System.Object constructor
3) Call the constructor of the subclass
Polymorphic
Polymorphic means that objects of the same type call the same method but exhibit different behavior.
Implementing method overrides using the virtual and override keywords
Hide base class members with new members
If you want to define a member in a derived class with the same name as the base class, you can use the New keyword to hide the base class members. If you do not use the New keyword, defining a member compiler with the same name as the base class in a derived class generates a warning.
C # does not support multiple inheritance
Classes in C #, structured class notes