Variables and types
C # is a strongly typed language, so each variable must declare the type. In the new C # version, you can also use VaR to declare it.
From Visual C #3.0, variables declared in the method range can have implicit type var. Implicit local variables are strongly typed (as if you have declared this type), but the type is determined by the compiler. The following two I declarations are functionally equivalent:
var number = 10; // implicity typed int number2 = 10; // explicity typed
The type of a variable includes the numeric type and the reference type. The numeric type includes the integer type and the non-integer type.
An integer is divided into different types based on the number of digits, including byte, short, Int, and long.
Non-integer types include char and string.
Variable declaration method
Variables must be declared first, then assigned values, and finally used.
The variable name must have a meaning, rather than I, J, K! Note that there is a meaning, not a variable prefix like the type in C.
1. Declare and assign values first.
Int num; num = 3; // This method is not standard and may cause problems. Avoid this method
2. assign values directly when defining variables.
Int num = 3;
3. Multiple variables are declared at a time, separated by commas.
Int num1 = 20, num2 = 30, num3 = 40; // instead of int I, j = 20, 30 (Python Mode)
Variable Declaration Rules-can only start with a letter, _ and @, so it cannot start with $
C # variables are case sensitive!
Char A = 'a'; char a = 'a'; // variables of A and
Variable naming rules
1. Camel naming method (camel): lowercase for the first word and upper for the other words
2. Pascal naming method: the first letter of each word is capitalized.
C # uses the camel method to name variables or parameters, while the namespaces and classes use the Pascal naming method.
Supplement: there is also a common C or C ++ naming method called the Hungary naming method (this method is not recommended in C # and other programming languages ).
However, the applied naming methods in Hungary naming methods are widely used! This method is often used especially for variables consisting of more than three words.
Refer:
This is the decline of the "Hungary name method" and the suggestion C # int
Pay attention to the Integer Range in C # Int.
Range:-2,147,483,648 to 2,147,483,647
Size: A signed 32-bit integer
Http://msdn.microsoft.com/zh-cn/library/5kzh1b5w.aspx
For example, can C # int store mobile phone numbers? Answer: No, because the length of the phone is beyond the int range.
C # char
In C #, char stores a character. Note that C # Is unicode encoded, so char can store a Chinese character.
Class program {static void main (string [] ARGs) {char firstname = 'zeng '; console. writeline (firstname); console. readkey ();}}
Use single quotation marks when assigning values to Char variables '.
However, it should be noted that the implementation of char characters is different for different systems, so the length of char is determined by the System Based on several bytes. for example, the char character in SQL Server occupies only one byte.
The definition of char in C # should be paid special attention to for those who have understood the C language but are not very proficient. Especially for people who have no or used knowledge, char occupies one byte in isolation. In fact, this definition is different for different languages, so pay special attention to it.
C # string
String definition: concatenates multiple characters.
String Length is the length of a string, that is, the number of characters.
String word = "I am it male"; console. writeline (word. Length); // 5
Float, double, and decimal
Float is a 32-bit floating point, and double is a 64-bit floating point. Decimal is a 128-bit data type, which is generally used to store money. Note. Although decimal has multiple digits, its range is smaller than double or even smaller than float, but its precision is the highest.
In addition, note that the default type of a real number with a decimal point in C # is double, and the decimal type must be defined using M/M. F/D/M are used to declare different floating point numbers.
Float salary = 7600.33; // if F is not added here, the compilation will fail.
Decimal salary = 7600.33; // It cannot be compiled here. m must be added.
Type conversion
Type conversion includes implicit conversion and explicit forced conversion, help conversion functions, and conversion classes.
Implicit conversion:
Float number = 10; // can be converted only from low to high.
Forced conversion:
Int num = (INT) 3.14; // the conversion of values with different precision may lose the precision.
Help conversion functions:
Int. parse ("123"); // if the value is not interpreted or beyond the range, will the compilation be normal, but an exception formatexception or overflowexception will be thrown during runtime.
Exercise
String name = "XX it male"; int age = 33; char sex = 'male'; string ensex = "men"; // use double quotation marks short sexflag = 1; // 1 indicates male and 2 indicates female console. writeline ("My name is {0}" + "Age is {1}", name, age );