Class Member
Fields and methods are the most important class member types, fields are data members, methods are function members
Field
A field is a variable that belongs to a class
- It can be of any type, whether it is a predefined type or a user-defined type
- As with all variables, fields are used to hold data
- They can be written
- They can be read
Method
Method is an executable code block with a name
The simplest syntax for declaring a method is as follows
- return type, if a method does not return a value, the return type is specified as void
- Name
- Parameter list
- Method body
Creating instances of variables and classes
- Class is a reference type
- References to data are stored in variables of class type
Constants and static quantities
Although a constant member appears to be a static quantity, you cannot declare a constant as static
Property
property is a member of a data item that represents an instance of a class or a class
Similar to fields, attributes have the following characteristics
- It is a named class member
- It has a type
- It can be assigned value and read
Unlike fields, however, a property is a function member
- It does not allocate memory for data storage
- It executes the code
Property declarations and accessors
The set accessor is always
- Have a separate, implicit value parameter with the name value, the same as the property type
- Has a return type void
Get accessors are always
- No parameters
- Have a return type that is the same as the property type
Perform other calculations
A property accessor is not limited to passing an outgoing parameter to an associated fallback field only, the accessor can perform any calculation, or no calculation is performed, the only necessary behavior is that the get accessor returns a value of the property type
In the following code, the set accessor implements the filter before setting the associated field, and if the input is greater than 100, it is set to 100
int Ten ; int X { getreturn _x;} Set - - : Value;} }
View CodeRead-only and write-only properties
- Properties that are only get accessors are called read-only properties
- Only the property of a set accessor is called a write-only property
- At least one of the two accessors should be defined, or the compiler will produce an error message
Example of calculating a read-only property
The following code shows the read-only property
int 3 ; int 4 ; int Z// read-only property {getreturn (_x+_y);} }
View Code
The value of z can only be generated based on values of _x and _y
Auto-implemented Properties
The auto-implemented attribute points are as follows
- Do not declare a fallback field
- Method body that cannot provide accessors
- Fallback fields are not accessible unless through accessors
Static properties
Properties can also be declared as static. The accessors for static properties and all static members have the following characteristics
- Instance members of the class cannot be accessed, although they can be accessed by instance members
- Whether the class exists or not, they all exist.
- When accessed from outside the class, it must be accessed using the class name instead of the instance name
ReadOnly modifier
Fields can be decorated with ReadOnly, whose action type is declared as const in the field, but cannot be changed if the value is set.
- The const field can only be initialized in the declaration of a field, and the ReadOnly field may be initialized at any of the following locations
- Field declaration statement, type const
- class, and if it is a static field, initialization must be done in the static constructor
- The value of the Const field must be determined at compile time, and the value of ReadOnly can be determined at run time
- Unlike the const, the const behavior is always static, while the following two points of ReadOnly are correct
- It can be an instance field, or it can be a static field
- It has a storage location in memory
this keyword
The This keyword is used in a class and is a reference to the current instance. It can only be used in code blocks of the following class members
- Instance constructors
- Instance method
- Instance accessors for properties and indexers
This is used for the following purposes
- Parameters for members and local variables for the zone classification
- As an argument to invoke a method
Indexer what is an indexer
An indexer is a set of get and set accessors, similar to properties.
Shows the representation of a class's indexer, which can get and set string values
Indexers and properties
Indexers and attributes are similar in many ways.
- As with properties, indexers do not allocate memory to store
- Indexers and properties are primarily used to access other data members that are associated with these members and do not provide access to and settings for them
- Property typically represents a separate data member
- Indexers typically represent multiple data members
There are a few things to note about indexers
- As with properties, indexers can have only one accessor, or both can have
- The indexer is always an instance member and therefore cannot be declared as static
- As with properties, the code that implements the accessor for get and set does not necessarily have to be associated with a field or property. This code can do anything or nothing, as long as the get accessor returns a value of a specified type
Declaring indexers
The syntax for declaring indexers is as follows
- The indexer does not have a name. The location of the name is the This keyword
- The argument list is in the middle of the square brackets
- At least one parameter must be declared in the parameter list
Declaring indexers is similar to declaring properties
The set accessor of the indexer
The set accessor has the following syntax
- It has a return type of void
- It uses the same argument list and indexer declaration
- It has an implicit parameter named value, with the same value parameter type as the index type
Get accessor for indexer
The get accessor has the following semantics
- It has the same argument list and indexer declaration
- It returns the same type of value as the indexer
The following code declares an indexer for class X
classX { Public stringA//calling Field 1 Public stringB//calling Field 2 Public stringC//calling Field 3 Public string This[intIndex] { Set { Switch(index) { Case 0: A=value; Break; Case 1: B=value; Break; Case 2: C=value; Break; default: Throw NewArgumentOutOfRangeException ("Index"); } } Get { Switch(index) { Case 0:returnA; Case 1:returnB; Case 2:returnC; default: Throw NewArgumentOutOfRangeException ("Index"); } } } }View CodeAccess modifiers for accessors
By default, two accessors for a member have the same access level as the member itself. That is, if a property has a public access level, its two accessors have the same level of access, as are the indexers.
However, you can assign a different level of access to two accessors. The following code demonstrates a very common example of declaring a set accessor as private and a get accessor declared as public. Get is public because the access level of the property is public.
In this code, although the property can be read from outside the class, it is only set inside the class.
class person { publicstringgetprivateset;} Public Person (string name) { this. name = name; } }
View Code
There are several restrictions on accessor access modifiers. The most important limitations are as follows
- An accessor can have an access modifier only if the member (property or indexer) has both a get accessor and a set accessor
- Although both accessors must appear, only one of them can have an access modifier
- The access modifier of the accessor must be more restrictive than the access level of the Member
Clarified the level of access levels. The access level of the accessor must be at a lower position in the chart than the member's access level
[C # Basic] class