Constant: A value that already exists at the time of program compilation and does not change during the lifetime of the program, defining a constant with Const.
Private class TestA -- define a class without a ()
{
Private Const double pi=3.14;
Private double GETPI ()--output pi is a worthwhile method, behind band ()
{
Double T=pi;
return t;
}
}
Variable:
The variables themselves are used to store specific types of data, and can change the data values stored in the variable at any time as needed. A variable has a name, type, and value. The variable name is the identifier of the variable in the program source code. The variable type determines the size and type of memory it represents, and the value of the variable refers to the data in the memory block it represents. During the execution of a program, the value of a variable can vary. A variable must be declared before it is used, that is, the type and name of the specified variable.
Variables can store value types, or they can store reference types
Value type variable: stores its data values directly, mainly including integer types, floating-point types, and Boolean types.
Value type variables are allocated in the stack and are therefore highly efficient, and the main purpose of using value types is to improve performance, which is divided into the following 3 types of values.
(1) Integer type
(2) floating-point type
(3) Boolean type
Reference-type variables: Store their data for reference, reference-type variables can refer to the same object, in which case the operation on one variable affects the same object referenced by the other variable. The value before the U reference type is assigned is null.
The difference between a value type and a reference type:
public class Test
{
public int a=3; --Value type variable
String ss= "1234455":
public string Sq=ss; --Reference type variable
}
Add a "?" after the variable type Represents a nullable value type.
For example:
into A=null;
The above code vs will prompt for error: cannot convert null to ' int ' because it is a type that cannot be considered a null value.
However, the following format is not an error:
Int? A=null;
A value type stores its value directly, whereas a reference type stores it as worthy of reference. Value types are stored in the stack of memory, and reference types exist in the heap of memory to the worthy reference addresses.
Fields, properties
A field is an element that makes up a class structure that can be declared not only by a C # built-in type, but also by any custom type, and it is clear that not only can you save an instance of a value type, but you can also save an address reference of a reference type. Fields can be defined directly in a class or struct. It has more features than constants, which can be not only the state data of the class, but also the state data of the instance, which is not static by default, but is a member of the object level unless it is explicitly specified that its modifier is static. Fields can use the modifiers: public, private, protected, internal, or protected internal. In addition, ReadOnly can also be used for fields, and if you add satic, it is equivalent to a constant, except that the object-level field initialization is done in the constructor, and class-level field initialization is done in the static constructor. The following code:
public class code_03 {public const double PAI = 3.14; Double radius =; static int a = ten; static readonly int b =; }
Constants are calculated at compile time, and the fields are evaluated at run time,
Const constants are initialized at the declaration, compiling values directly into the metadata at compile time, and the runtime cannot make value changes (such as Pai in the code below).
Instance fields can be initialized at the definition and within the constructor. Changes can be made anywhere, if their accessibility permits (such as RADIUS in the code below).
The static field is declared as a class-level field that belongs to the class's state data. Changes can be made anywhere, if their accessibility permits (as in the code below).
The ReadOnly field declares a read-only field and can only be changed within the constructor (such as B in the following code).
The static readonly field declares a statically read-only field, which belongs to class-level and read-only. You can only change it within a static constructor (such as C in the following code).
1 Public classcode_032 {3 Public Const DoublePAI =3.14;4 DoubleRadius = -;5 Static intA =Ten;6 ReadOnly intb =0;7 Static ReadOnly intc = -;8 9 Staticcode_03 ()Ten { OneA = -; Ac = +; - //Error non-static field, method, or property "Consoleapp.example03.code_03.c" requires an object reference - //radius = 1; the //B =-1; - } - Publiccode_03 () - { +Radius =1; -A =-1; +b =-1; A //error cannot assign a value to a static read-only field (except in a static constructor or variable initial value) at //C =-1; - } - Public voidMyMethod () - { -Radius =1; -A =-1; in //error cannot assign a value to a static read-only field (except in a static constructor or variable initial value) - //B =-1; to //C =-1; + } -}
View Code
Property:
Fields usually hold the state of the class or the object itself, and we can of course expose it as public to let the outside read and write the changes. In a sense, we prefer to maintain our own state within the class itself, and we do not want the outside world to make direct changes to our state to prevent the data from being destroyed, but fortunately there is a data member available, which is the attribute.
If you want to access an internal member of a class (a private field) externally, you can use the method to achieve the purpose, but it seems a bit cumbersome to write a method for each field to read and write. property provides a flexible way to access a private field, which is an "accessor" method, including the Get method and the set method, and more specifically, the implementation of the method's streamlined notation, which hides the code for implementation and validation. It has two accessors:
The get accessor is used to get the value of the property.
The set accessor is used to set the value of the property. Since it is a method and is to replace the private field with a new value within the method, it is possible (or should) receive the parameter, and the value keyword is used to define the value assigned by the set accessor. If there is a definition of the following attribute:
Public class Code_03_2 { string _name; Public string Name { getreturn _name;} Set {_name = value;} } }
View Code
This definition is accessed through the property name to the private field _name
We have seen that the property is the implementation of the method, since it is a method, the method can be accessed by the modifier definition, such as public, private, to limit the accessibility of the method, it is clear that we can also limit the accessibility of the property, here is the accessor set and get access qualification
Attributes also have a more concise way of writing the following:
public int Age {get; set;} public string Address {get; set;}
At compile time, the compiler will automatically generate the corresponding private fields _age and _address, as well as the corresponding get_ method and set_ method.
method, function
Before we know the way, we should look at classes and objects
Class: A collection of things of the same kind that have common properties and behaviors, such as humans, computer classes
Object: An instantiation of a class becomes an object, such as a Zhang San in a human being, an object of a class
define a human, with the name and age attributes Public classperson{ Public string_name; PbublicstringName {Get{ This. Name=_name;} Set{_name=value;} } Public string_age; Public stringAge {Get{ This. Age=_age;} Set{_age=value;} }}
View Code
The upper code is a human, with a name and age, two attributes.
So how did Zhang three come out of it?
Instantiation:
Person person =new person ();
Person. Name= ' Zhang San ';
Person. age=20;
(It can also be abbreviated as: Person Person=new person () {name= ' Zhang San ', age=20};)
So we're going to be honest about a human object, Zhang San.
And the method is what these human beings can do, defined in the interior of the class, such as Zhang San can drink water, give birth to children
Public classperson{//shorthand for attributes and fields at compile time claims the name of the field and the Get Set method Public stringname{Get;Set;} Public intage{Get;Set;} Public stringHeshui () {stringName= This. Name; returnname+"can drink water"; } }
View Code
Person Person=new person () {name= ' Zhang San ', age=20};
Person.heshui ();
Heshui is a method of the person class, and after instantiating an object it is possible to call the Heshui () method.
Constants, variables, methods, and so on generally have access modifiers to restrict access
The commonly used access modifiers are:
Private member, which can be accessed within the class
Public: Publicly available, completely public, without access restrictions
Protected: Protect members, which can be accessed in the inner and inheriting classes of a class
Internal: can be accessed within the same namespace
C # constants, variables, fields, properties, methods