Chapter2 Learning The C # language
This chapter will outline the C # language. The contents of the discussions include data types, operation methods, code, cycles, specific logic required. Although you don't need to learn all the details, this is the difference between occupation and amateur. Suggestions for learners, you can Unnecessary to remember this chapter, you can return to this chapter when you need it.
The .net languages
Using C # instead of VB.NET is just a change in development, but does not affect other, you can also use the language provided by third parties, but you need to remember that .NET Framework only knows the intermediate language, not Understand other languages. So use any language written in any language, can't be run in CLR before being compiled into an intermediate language.
C # language Basics
Case Sensitivity
Some languages are case sensitive, such as C, C #, Java, and VB.NET is not, if you use VB.NET, if you don't pay attention to case sensitive, if you use VB.NET, if you don't pay attention to your sensitive sensitive language, you don't pay attention to your sense of size, compile the compiler. It is not possible to pass. C # language is lower-written.
Comforting
Note Enhance the readability of the program code, he will not be compiled by the compiler, and C # provides two annihilation methods, the first kind of "//" After writing comments, the content after "//" will be Note, the content between the second / * and * /, the content between them will be treated as a comment.
C # also has an XML-based annotation, this format is very similar to the language of HTML, the format is ///
The content between them will be displayed as an instructions for the instructions for a button.
Line Termination
Any statement of the C # language is ";" ending, if there is no ";", he will also count the next line as this statement until ";".
For example: myValue = myvalue1 myvalue2 myvalue3; myvalue = myvalue1 myvalue2 myvalue3; myvalue = myValue1 myValue2 myValue3; is the same, they represent, executing the same action.
Block strCTures
The C # code is placed between {}, the code between {} is seen as a whole, {} can also have {}, and {} is paired, this is better than the beginning of the article "{" The end of the article "}".
Variables and data type
Any language can define a variable, the variable is used to store the corresponding data information. When using the C # defining variable, first give the variable, then give the correct type, these types include, text, data, time, pointer, class, etc. Wait. Example Int ErrorCode defines an integer variable named ErrorCode.
On the different language defined variable types of different languages, Microsoft proposed a basic variable type.
Basic variable type, VB variable type, C # variable type table is as follows:
Table 2-1.Class Library Name VB Name C # Name ContainsByte Byte byte An integer from 0 to 255.Int16 Short short An integer from -32,768 to 32,767.Int32 Integer int An integer from -2,147,483,648 to 2,147,483,647.Int64 Long long An integer from About -9.2e18 to 9.2e18.single Single Float A Single-Precision Floating Point Number from approximately -3.4e38 to 3.4e38.double Double Double A Double-Precision Floating Point Number from approximately -1.8e308 to 1.8e308.Decimal Decimal decimal A 128-bit fixed-point fractional number that supports up to 28 significant digits.Char Char char A single 16-bit Unicode character.String String string A variable-length series of Unicode characters. Boolean Boolean Bool A True Or False Value.Datetime Date * Repesents Any Date and Time From 12:00:
00 am, january 1 of the year 1 in the Gregorian Calendar, To 11:59:59 PM, December 31 of the year 9999. Time Values Can Resolve Values To 100 Nanosecond Increments. INTERNALLY, THIS DATA TYPE IS Stored AS A 64- Bit Integer.TimeSpan * * represents a period of time, AS in Ten Seconds or Three Day Day, The Smallst Possible Interval IS 1 Tick (100 nanoseconds) .Object Object Object The Ultimate Base Class Of All .Net Types. * If The Language Does Not Provide An Alias for a Given Type, You Can Just Use The .NET Class Name. * Representative, this type There is no definition in this language, but this type is actually existing. Of course, it is also possible, such as C # To use the TimeSpan type, you can define, timespan mytime; / * I want to use the TimeSpan type, you must add Previous prefix, such as system.time.timespan, this is not necessarily correct, just to make a bit * / other existing data types can also be defined.
Assignment and Initializers
Once variables are defined, the variables are assigned, such as int erroorcode; erroorcode = 10; regarding variable definitions, you can view the definition of the C language and assignment. But you need to pay attention, for example: decimal myden = 14.5;
Decimal is that the data range is about 1.0 * 10 ^ -28 to 7.9 * 10 ^ 28. Decimal's bit is 128. The number of digits is 64, then why decimal has no Double representation, this is Because the floating point number is divided into two parts: the mantissa and the degree (index), indicating that the number of digits of the mantissa is high, indicating that the number of bits of the index is large, although 64 bits, but its precision is low, so it can The range indicated, although Decimal is 128, but because it uses more bits to indicate its accuracy, they have to sacrifice the scope. Write this in Microsoft's help book, the Decimal keyword represents 128-bit data types. Compared to floating point, the Decimal type has a higher precision and smaller range, which makes it suitable for financial and currency calculations. Above this example, you will automatically define myDecial as a double type when you assign a value. If you want to avoid this, you should add Decimal myDecimal = 14.5m when you define; why the defined decimal type variable will be converted when assigning values. Cheng Double type? I know the master still hope to advise! Thank you!
Strings and escaped characters
"/" And some words are in line with it, so "/" can also be called a escape character, * How to use it later how to use *, for example
Path = "c: // myapp // myfiles"; it and PATH = @ "c: / myapp / myfiles"; what is the same, therefore, @ character means ignoring the "/" character after @.
Arrays
Array, this is a variety of languages, which can define a series of data, such as int types, and the starting number of arrays is 0. Example:
String [] stringArray = new string [4];
Here, 1 string array is defined here. The name of the array is StringArray. There are 4 members (rather than 5, this problem is easily confused), the way the access array is, for example, the first one of the members of the array It is StringArray [0], the second is StringArray [1], and it is pushed.
Of course, you can also define a one-dimensional array, a two-dimensional array, and a three-dimensional array. They all follow the rules above.
When the array is defined, but when it is not initialized, they will be default 0 or false.
As an example of two-dimensional array, define a two-dimensional array. Should be defined below:
INT [4, 2] intArray = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}; / * Do not have to follow this assignment rule, this is two values Assignment, you can also understand the EXCEL form, INT [4, 2], 4 represents the number of columns, 2 represents the number of columns, 2 represents the number of columns, so this number is defined in the number of columns. The first is Intarray [0,0], the last one is Intarray [3, 1].
The arraylist
This is a new thing for me. I talk about my understanding. This can be said to be a special array type. You can use him to define an array, but you can't use him before you change him. Because he does not have a specific type, he may be int, maybe a char, maybe it is string. You can put it in the data you want, and then turn him again when you need to use it, it seems that I express it. So let me give an example.
ArrayList Dynamiistlist = New ArrayList (); DynamiSt.Add ("One"); DynamiSt.Add ("One"); DynamiSt.Add ("One");
In this way, there is three in the DynamicList array. These three are one; string item = convert.tostring (DynamiSt [0]); int myValue = convert.toint (DynamiSt [1]); / * TOINT is my guess , I don't know if there is this method, I am a class push * / bool myboolean = convert.tobool (DynamiSt [2]); / * Tobool is also guess * / I think, except the first one is ONE string The second is a number, the third is the number of BOOL.
Therefore, this array breaks the data defined in an array must be such a specification of the same type, you can put numbers, characters, strings, etc. in the ArrayList type. I think this is the necessary of this array type.
ENUMERATIONS
Enumeration, and a little similar, but there is different, the class has a method, but he is not, for example;
ENUM UserType {admin, guest, invalid} / * This will be automatically assigned, and admin is assigned 0, GUEST is 1, INVALID is 2, you need to pay attention to ",", ", not"; " * /
Usertype newusertype = usepe ..admin;
You can also assign a value in autonomous, you only need to add = and numbers behind, such as admin = 80;