In C # We are free to access common fields, but sometimes we may need a field that can only be read or write, or something else to do when the field is changed, and it is obvious that these fields are simply not possible, and then there are attributes.
1. Basic usage
Properties in C # consist of Get scopes (access methods) and set scopes (access methods) defined in the property scope.
class test{ privateint number ; // an int represents the data type that the property encapsulates // The data type must be the same as the corresponding field (number) Public int Num { get{return number ;} Set {number=value;}} }
When you encapsulate data, you may want to configure a read-only property. To do this, the set block can be ignored. Similarly, if you want to have a read-only property, the get block is ignored.
2. Static properties
C # Static properties can be either read-write properties, read-only properties, or write-only properties. Static accessors apply to an entire class, not an instance of a class. The set,get accessor in a static property can only access static members of the class. Static properties can be accessed through the class name and cannot be invoked using an instance, as is the case with static methods.
1 //Simple savings Account class2 classSavingaccount3 {4 //instance-level data5 Public Doublecurrbalance;6 7 //static data points8 Private Static DoubleCurrinterestrate =0.04;9 Ten //Static Properties One Public Static DoubleInterestRate A { - Get{returncurrinterestrate;} - Set{currinterestrate=value;} the } -}
3. Automatic attributes
Automatic attributes simplify the process of encapsulating data fields, easing the work of defining private return fields and related C # attribute members. When defining an automatic attribute, only the access modifier, the actual data type, the property name, and the empty get/set scope are specified. At compile time, the compiler automatically generates a private return field for the type and the appropriate Get/set implementation logic.
1 classEnemy2 {3 //Automatic Properties4 Public stringname{Get;Set;}5 Public intlife{Get;Set;}6 Public floatattack{Get;Set;}7 8}
Note: Unlike traditional properties, it is not allowed to build automatic attributes that are read-only or write-only, and automatic attribute definitions must support both read and write capabilities. But it's possible to define a more restrictive get or set.
4. About automatic attributes and default values
You can use automatic attributes that encapsulate numeric or boolean data directly in your code, because the hidden return field sets a safe default value that you can use directly. However, if the automatic attribute encapsulates another class variable, the default value of the hidden private reference type is set to NULL.
For example, the following class spawn, using two automatic attributes
1 class Spawn 2 3 // hidden int field default value is 0 4 public int Numbers{get ; set ;} 5 The Hidden Enemy return field is null 6 public Enemy Myenemy{get ; ;} 7 }
When Myenemy is called directly, a null reference exception is obtained at run time because no new object is set for the enemy member variable used in the background.
Because the private return field was created at compile time, the reference type cannot be directly assigned directly using the New keyword, and must be executed inside the constructor of the class to ensure that the object is born in a safe manner.
1 classSpawn2 {3 //the default value for the hidden int field is 04 Public intnumbers{Get;Set;}5 //The hidden enemy return field is null6 PublicEnemy myenemy{Get;Set;}7 8 //the default value assigned to the hidden return field must be overridden with a constructor function9 PublicSpawn ()Ten { OneMyenemy =NewEnemy (); ANumbers =1; - } - the PublicSpawn (Enemy Enemy,intnum) - { -Myenemy =enemy; -Numbers =num; + } -}
Properties in C #