Variables, constants and types

Variables, constants and types

variable

Variable is actually a break point, which references a memory address, but the value it stores may vary.

Variable declaration

Single variable declarations: string yourName;
a plurality of the same type variable declarations: string x, y, z;
a plurality of different types of variable declarations:

string x, y;
double z;

: May be initialized when the statement string yourName = “eric”;
may be selectively initialized Statement:string x = “x”, y, z;

Variable initialization

Some requirements of variable initialization

  • All local variables before being initialized explicitly, if it will be thrown exception Compiler Compiler
  • All fields compiler stage variable is initialized to the default value belongs types

Variable scope

  • Scopes field which is equivalent to the scope of the class belongs to the class of the field is located
  • Is limited to the scope of local variables declared method of its inner loop, braces {} is bounded

Overlapping scopes

  • The same scope, there are two variables of the same name, do not care about the type of variables here are the same. If this is the case, the compiler will report an error, the compiler will not continue.
  • Local variables and field-level variables of the same name, the same name as a local variable will hide the field-level variables, that is within the scope of local variables, local variables covering the value of the field-level variables.

constant

  • Initialization immediately upon declaration, its value can no longer be changed after initialization
  • Use explicit type declaration, you can not use the keyword var
  • May be declared in the class, structure, interface
  • Can be used as a class, structure, field interface, it can be defined in the class, the local variables of the internal structure of the method, in fact, the constant is always static, although did not use the static keyword (not allowed)
  • Variables can not accept the assignment, even if the variable is static and readonly is not OK, another constant can only be used when it is initialized to the assignment, of course, directly giving a specific value better

The difference between static and dynamic constants Constants

This section content reproduced from static and dynamic constants constant difference

There are two ways to define constants in C # mode:

  • Static const (Compile-time constant)
  • Dynamic constants (Runtime constant)

The former with "const" is defined, the latter with "readonly" is defined. For static constant (Compile-time constant), which is written as follows:

public const int MAX_VALUE = 10;

Why call it a static constant, because the above statement can be understood as follows (Note: The following writing is wrong, will be a compilation error, here for convenience only).

public static const int MAX_VALUE = 10;

With constant const defined for all class objects are the same, it is necessary to access static members like to visit as constant const defined, but with the way the members would object to access a compilation error. In addition, access to static constants at compile time, is used to replace the constant values ​​of the constants, such as:

int nValue = MAX_VALUE;

After compiling the phrase, and the phrase following the intermediate language code generated is the same.

int nValue = 10;

However, when used to define constants const, there are many restrictions on the type. First, this type must be of simple data types (built-int and float, string, or enumeration), while this type of new initialization can be accomplished by, so some type constant value nor can a struct definition const defined.

const relative terms, to be used to define the constant readonly more flexible, it is written as follows:

public readonly int MAX_VALUE = 10;

Why is it called dynamic variables, constants because the system allocates space to be defined as readonly, that and the other members of the class as an independent space. Further, readonly defined constant except when the defined constant value may be set, but also can be set in the class constructor. Since members of defined constants readonly equivalent to class, so use const to define the type suffered constant limit, all disappear when using readonly to define, that can be used to define any type of readonly constants. Integrated above, as the difference between the two comparison follows.

readonly constants can only be declared as class field, supports the instance type or static type , can be initialized at the same time declared or initialized in the constructor, after completion of initialization can not be changed. In addition to the constant const can be declared as class field, you can also declare a method in the local constants, default static type (no need to use static modification, otherwise it will cause a compiler error), but at the same time must be completed initialization statement.
Comparison of the two

Static const Dynamic Constants
definition At the same time declared to set a constant value May declare that need not be set constant value, can be set in the constructor of the class
Type restrictions First type must be of the type of range value, and its value can not be set by the new There is no limit, it may be used in any type of constant definitions. For the class object for all classes of objects, the value of the constant is the same. For different object classes, the constant value may be different.
Memory consumption no To allocate memory to save the literal entity
Overview Performance is slightly higher, no memory overhead, but many restrictions, is not flexible Flexible, convenient, but slightly lower performance, and there is memory overhead

For the definition of the time constants, in the end is defined by const or readonly be defined, in order to pursue my previous performance, so far as possible be defined by const. But in this book, he mentions about the use of const creates the potential bug. When using the static constant is a class library DLL in the program, if you modify the value of the static constant in the library, the other interface does not change, in general, the program calls end is no need to recompile, you can direct execution call the new class library. But that is in this case, will have a potential bug. This is due to the constant static at compile time, its value is used to replace the constant, so call to end the program in the same way for replacement. For example: it defines a static constant in the library, as follows:

public const int MAX_VALUE = 10;

So call this static constant for the program code, the intermediate language code generated by the compiler, the place is to be replaced by 10, that is, using the static constant, instead of 10. So when the library static variable changes, for example:

public const int MAX_VALUE = 15;

Then run may be made without recompiling the program to call the end, but this time the intermediate language code corresponding to the program is the value of the static variable 10, instead of the new class library 15. Therefore thus produced inconsistent, the program will lead to a potential bug. Way to solve such problems, is to call the end of the program after updating the library recompile, which generates a new intermediate language code.

When the potential bug as defined constant const is present, when the constants are defined with readonly not occur. Because the members of the class of readonly constant is similar to the definition, requiring constant according to the specific address to access the time of the visit, in order to avoid such a bug.

In view of this, it is proposed to replace the const readonly to define constants.

type of data

C # built-in types bool, byte, sbyte, char, decimal, double, float, int, uint, long, ulong, object, short, ushort, string, divided value type (also called simple type) and reference types

Value Type

Structure type, numeric type (integer (sbyte, byte, char, short, ushort, int, uint, long, uling), float (float, double), the decimal type (decimal)), Boolean (bool) and custom structure types and enumerated types

Integer initialization, if the integer is not a suffix, then it is a type of the following types may represent the value of a first type: int, uint, long, ulong, and can be specified according to the suffix, L or l represents, then the actual size of to determine whether long or ulong, or the u-u represents the actual size, which is determined according to the type ulong uint or, if in combination with UL, said ulong

When initialization floating decimal default is considered double, double myDouble = 2.3; integers are treated, if desired double, with the suffix d or D, double x = 10d; real number is considered as desired decimal type, and the suffix m or M , decimal myMoney = 1023.5m; wish to be regarded as float, the suffix f or f, float y = 3.14F;

Mark with upper and lower case, but it is recommended to use uppercase, lowercase L, after all, easily confused with 1

Reference types

Reference type of variables are stored in the managed stack, while the actual data is stored in the managed heap, mainly composed of an interface, an array, Object type, class, delegate, string, null type, in addition to a pointer type (only unsafe used in the context tag), dynamic type (in most cases the same object, wherein different compilers for expression of type dynamic type checking, and not further resolved, in fact, will be compiled into object, therefore only present in the compile It does not exist at runtime)

References

[1]: C # The Definitive Guide
[2]: static and dynamic constants constant difference

Guess you like

Origin www.cnblogs.com/home-wang/p/10943092.html