".NET Programming Pioneer C #" Chapter 4 C # Type

zhaozj2021-02-17  54

".NET Program Pioneer C #" Chapter 4 C # Type Christoph Wille | 2001-9-1

Chapter 4 C # Type Since you know how to create a simple C # program, I will introduce you to the C # type system. In this chapter, what you learned how to use different values ​​and reference types, plus boxes and framework mechanisms can do for you. Although this chapter does not focus on example, you can learn a lot of important information about how to create an existing type of program. 4.1 Value Type Value Types Always contain a value of the type of type. C # forces your initialization variables to use them to calculate - the variable is not initialized, because when you try to use them, the compiler will tell you. This value is actually copied whenever a value is assigned to a value type. Compared to the reference type, it is only copied by the reference, and the actual value remains in the same memory location, but now there are two objects to point to it (reference it). The value type of the C # can be classified as follows: • Simple Types · Struct Types · Enumeration Types 4.1.1 Simple Type The Simple Type of C # Sharing some features. First, they are all the alias of the .NET system type. Second, the constant expression consisting of simple types is only detected when compiling is compiled rather than runtime. Finally, the simple type can be initialized by literal. The following is a C # simple type class: • Integer · Boolean · Character type (a special case of integer) · Floating-point · score 4.1.1.1 integer C # is intellectually. Sbyte, Byte, Short, Ushort, Int, Uint, Long, Ulong, and Char (discussed separate section). They have the following characteristics: Sbyte type is a symbol 8-bit integer, and the value ranges from 128 to 127. The ByTET type is unsigned 16-bit integer, and the value ranges between 0 and 255. · The Short type is a symbol 16-bit integer, with a value between -32, 768 ~ 32, 767. · The ushort type is unsigned 16-bit integer, and the value ranges from 0 to 65, 535. The int type is a symbol 32-bit integer, and the value ranges from -2, 147, 483, 648 ~ 2, 147, 483, 647. • The uint type is unsigned 32-bit integer, and the value ranges from 0 to 4, 294, 967, 295. • Long type is 64-bit symbolic integers, with a value range between 9, 223, 372, 036, 854, 775, 808 ~ 9, 223, 372, 036, 854, 775, 807. • Ulong is 64-bit unsigned integers, with a value between 0 to 18, 446, 744, 073, 709, 551, 615. VB and C programmers may be surprised by the new range represented by the INT and Long data types. Compared to other programming languages, in C #, int is no longer depends on the size of a machine (Word), and long is set to 64 bits. 4.1.1.2 Boolean Boolean data types have two Boolean values ​​of True and False. Can be assigned to a true or false value or may be paid to an expression, the value of which is equal to one of both: bool btest = (80> 90); compared with C and C , in C # The TRUE value is no longer any non-zero value. Don't convert other intersive into a block in order to increase convenience. 4.1.1.3 Character character type is a single Unicode character. A Unicode character is 16-bit long, which can be used to represent a variety of languages ​​in the world.

You can assign a character variable to a character variable as follows: CHAR ChsomeChar = 'a'; in addition to this, the variable assignment can be given to the variable (prefix / U) by hexadecimal escar (prefix / x) or Unicode representation: char ChsomeChar = '/ x0065'; char chsomechar = '/ u0065'; there is no implicit conversion to convert char to other data types. This means that in C # treats a character variable as an additional integer data type, it is not good - this is another aspect of the C programmer must change the habit. However, explicit conversion can be used: char chsomechar = (char) 65; int nsomeint = (int) 'a'; there is still an escaper (character meanings) in C. To change your brains, see Table 4.1. Table 4.1 escaping (escape sequences) escape character name / 'single number / "double quotation mark // backslash / 0 empty character / A exclamation number (ALERT) / B retract / F change page / N new row / R Enter / T Horizontal Tab / V Vertical Tab 4.1.1.4 Floating-Point Two Data Types is used as floating point: float and double. Their difference is the range and accuracy: float: Value range in 1.5x10 Between ^ -45 ~ 3.4x10 ^ 38, the accuracy is 7 digits. Double: The value ranges between 5.0x10 ^ 324 ~ 1.7x10 ^ 308, the accuracy is 15 ~ 16 digits. When using two floating points When the operation is performed, the following values ​​can be generated: the finite number of positive and negative non-digital values ​​(not-a-number, abbreviations NAN) non-zero value is another calculation rule for the expression One value in the float is a floating point, all other types are converted into floating point to perform operations. 4.1.1.5 The decimal type decimal type is a high-precision, 128-bit data type, it It is intended for the calculation of finance and currency. It is represented by about 1.0x10 ^ -28 to 7.9x10 ^ 28, with 28 to 29 valid numbers. To note that accuracy is in a bit number (DIGITS) instead of decimal Decimal Places, is said. The calculation is accurate to the maximum of 28 small digits. As you can see, its range value is narrower than Double, but it is more accurate. So there is no DECIMAL and DOUBLE Implicit conversion - a directional conversion may overflow, and the other direction may lose accuracy. You have to use explicit conversion. When a variable is defined, use the M suffix to indicate that it is a decimal type : Decimal DecmyValue = 1.0m; if M is omitted, it will be compiled as a Double type before the variable is assigned. 4.1.2 Structure Type A structural type can declare constructor, constant, field, method, attribute, index , Operators and nested types. Although the functions listed like a mature class, the difference between structures and classes in C # is that the structure is a value type, and the class is a reference type. Compared with C , You can define a class with a structural keyword. The main idea of ​​using the structure is to create a small object such as Point and FileInfo. You can save memory because there is no additional reference to the same reference. For example, this will cause great differences when declaring the arrays of thousands of objects. Listing 4.1 contains a simple structure named IP, which represents 4 use byte type 4 IP address of a field.

I don't include methods, because these jobs are just a detailed description in the next chapter. Listing 4.1 Define a simple structure 1: use system; 2: 3: Struct IP4: {5: Public Byte B1, B2, B3, B4; 6:} 7: 8: Class test9: {10: public static void main 11: {12: IP myip; 13: myip.b1 = 192; 14: myip.b2 = 168; 15: myip.b3 = 1; 16: myip.b4 = 101; 17: console.write ("{0 }. {1}. ", Myip.b1, myip.b2); 18: console.write (" {0}. {1} ", myip.b3, myip.b4); 19:} 20:} 4.1. 3 Enumeration Type When you want to declare a unique type consisting of a specified constant collection, the enumeration type is what you want to find. The simplest form, it looks like this: Enum Monthnames {January, February, March, April}; because I used default settings, the enumeration element is an int type, and the first element is 0 value. Each continuous element is incremented by 1. If you want to assign a value directly to the first element, you can set it into 1: Enum Monthnames {January = 1, February, March, April}; if you want to give an intention to give each element - even the same value - - This also has no problem: Enum Monthnames {January = 31, February = 28, March = 31, April = 30}; the last choice is different from int. Data type. You can assign a value in a statement: enum monthnames: byte {january = 31, february = 28, march = 31, April = 30}; you can use only LONG, INT, SHORT, and BYTE. 4.2 Reference Types and Value Types, the reference type does not store the actual data they represent, but they store references to actual data. Provide the following reference types in C # to use: • Object type · Class type · Interface · Representative Yuan · String Type · Array 4.2.1 Object Type Object Type is the mother of all types - it is the most fundamental base of other types class. Because it is the base class of all objects, any type of value can be assigned to it. For example, a integer: Object theobj = 123; give all C programmers a warning: Object does not equivalent to Void * you might be looking for. In any case, forget the pointer is always a good idea. When a value type is fetched (as an object is utilized), the object type is used. This chapter will discuss it later to fetch and fax 4.2.2 Class Types A class type can include data members, function members, and nested types. Data members are constants, fields, and events. Functions include methods, attributes, indexes, operators, constructor, and destructors. The functions of classes and structures are very similar, but as mentioned earlier, the structure is a value type and the class is a reference type. Compared to C , only single inheritance is allowed. (You can't have multiple base classes that derive a new object.) However, a class in C # can be derived from multiple interfaces, which will be described in the next section. Chapter 5 "Class" special discussion use class programming. This section is only intended to give the C # class where to fit a full picture of the type diagram. 4.2.3 Interface An interface declares a reference type of abstract members. Similarly to C is: a member of a structure, and a method is equal to 0. If you don't know anything about the concept, here is what the interface is actually done in an interface in C #.

There is only a method mark, but there is no code at all. This suggests that you cannot instantiate an interface, you can only instantiate an object derived from the interface. Method, attributes, and indexes can be defined in an interface. So, what is the speciality of the interface? When defining a class, you can derive from multiple interfaces, and you can only be derived from only one class. You may ask: "OK, but I have to implement all the interface members, then what can I get from this way?" I want to give an example from .NET: Many classes implement the IDictionary interface. You can use a simple type to convert access interface: idictionary mydict = (iDictionary) SomeObjectThatsupportsit; Now your code can access the dictionary. Also, I said that many classes can implement this interface - so you can reuse the code in multiple places to access the IDictionary interface! Once you have learned, you can use anywhere. When you decide to use an interface in class design, learn more about object-oriented design is a good idea. This book can't teach you these concepts, but you can learn how to create an interface. The following code segment defines the interface ifce, which only has a method: Interface ifce {void showmyface ();} As I mentioned, you cannot instantiate an object from this definition, but you can derive a class from it. Therefore, this class must implement the ShowmyFace abstract method: Class CFace: ifce: IFACE () {Console.Writeline ("Implementation");}} The difference between interface members and class members is that the interface member cannot be implemented. Therefore, I don't want to mention this again in the next chapter. 4.2.4 Representing a representative element packaged a method with some markers. Basically, representative meta is a security version (callback function) of type security and function pointer. Static and instance methods can be simultaneously packaged in a representative element example. Although you can use a representative as a method, its main use is to have a class event. Again, I want to introduce you to the next chapter, where you will discuss classes in detail. 4.2.5 String Type C The programmer may be surprised, but of course, C # has a basic string type for operating string data. The string class is derived directly, and it is sealed, which means that you can't derive classes from it. Just like other types, strings are an alias for predefined class System String. Its usage is simple: string mystring = "some text"; the merge string is equally simple: string mystring = "some text" "and a bit more"; and if you want to access a single character, what you want to do is access to the subscription : Char chfirst = mystring [0]; When comparing whether the two strings are equal, simply use the "==" comparison operator. IF (MyString == YourString) ... I just want to mention that although the string is a reference type, it is a comparison value, not a comparison reference (memory address). String types are almost used in every example of this book, and in these routines, I will introduce some of the extreme interesting methods that are revealed by string objects. 4.2.6 Array An array contains variables accessed by calculating the subscript. All variables contained in arrays and as elements must be the same type. This type is naturally called "array type". Arranges can store integer objects, string objects, or any object you propose.

The number of dimensions of the array is the so-called row, which determines the number of related array elements. The most common array is a one-dimensional array (first row). A multi-dimensional array has a ranks greater than 1. The subscript of each dimension starts at 0, and finally the length of the dimension is 1. There should be sufficient theoretical support. Let's take a look at the array initialized with an array initializer: string [] arrlanguages ​​= {"c", "c ", "c #"}; this shorthand effect is equal to the following: arrlanguages ​​[0] = "c" Arrlanguages ​​[1] = "C "; Arrlanguages ​​[2] = "C #"; the compiler has done all your work for you. Of course, it will work with the multi-dimensional number of initializer: int [] arr = {0, 1}, {2, 3}, {4, 5}}; it is below: Arr [0,0] = 0; Arr [0, 1] = 1; Arr [1,0] = 2; Arr [1, 1] = 3; Arr [2, 0] = 4; Arr [2, 1] = 5; if you I don't want to initialize an array in advance, but I know its size, this statement is like this: int [] myarr = new int [5, 3]; if the size of the array must be dynamically calculated, the statement used for array can be created Image Writing: Int nvar = 5; int [] arrtoo = new int [nvar]; as I started in this section, you can come to anything in the array, as long as all element types are the same. Therefore, if you want to put anything in an array, declare its type as an object: 4.3 In the course of filling this chapter, I have given a wide range of value types and reference types. Due to the speed of the reason, you will use value type - it is nothing more than the memory block occupying a certain space. However, sometimes the convenience of objects is as good as the type value type. This is where the box and the box are boarded on the stage, and the boxes and boxes are the core concept of the C # type system. This mechanism forms a bundled connection between value type and reference type by allowing a value type to convert into a type object or from a type object. Anything is one thing is an object - however, only when they need them being an object. 4.3.1 Box Convert to a Value Pixture Refers to the implicitly converting any value type into a type object. When a value type is fetched, an object instance is assigned, and the value of the value is copied to a new object. See the following example: int NFunny = 2000; Object Ofunny = NFunny; the second line of assignment hints to call a plus box. The value of the NFunny integer variable is copied to the Ofunny object. Now the integer variables and object variables are exist at the same time in the stack, but the value of the object remains in the heap. So, what does it impose? Their values ​​are independent of each other - there is no connection between them. (Ofunny does not quote the value of NFunny.) The following code descriptions: int NFunny = 2000; Object Ofunny = nfunny; Ofunny = 2001; console.writeline ("{0} {1}", nfunny, ruinny; when code change When Ofunny's value, NFunny's value has not changed. As long as you have this COPY action in your head, you can use the value of the value of the value of the value to play your huge advantage! 4.3.2 Following Conversion and Pixabay Compared to Follow Followering Operation - You must tell the compiler, and you want to extract which value type from the object. When a fax operation is performed, the C # detects the requested value type is actually stored in an object instance.

转载请注明原文地址:https://www.9cbs.com/read-29924.html

New Post(0)