Addison essential C# 4.0 Visual Studio_2
Số trang: 98
Loại file: pdf
Dung lượng: 1.79 MB
Lượt xem: 7
Lượt tải: 0
Xem trước 10 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu addison essential c# 4.0 visual studio_2, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Addison essential C# 4.0 Visual Studio_2 51 null a nd void To actually change the value in text, assign the value from ToUpper()back into text, as in the following: text = text.ToUpper();System.Text.StringBuilderIf considerable string modification is needed, such as when constructing along string in multiple steps, you should use the data type System.Text.StringBuilder rather than string. System.Text.StringBuilderincludes methods such as Append(), AppendFormat(), Insert(), Remove(),and Replace(), some of which also appear on string. The key difference,however, is that on System.Text.StringBuilder these methods willmodify the data in the StringBuilder itself, and will not simply return anew string.null and voidTwo additional keywords relating to types are null and void. null is avalue which indicates that the variable does not refer to any valid object.void is used to indicate the absence of a type or the absence of any valuealtogether.nullnull can also be used as a type of string “literal.” null indicates that a vari-able is set to nothing. Reference types, pointer types, and nullable valuetypes can be assigned the value null. The only reference type covered sofar in this book is string; Chapter 5 covers the topic of creating classes(which are reference types) in detail. For now, suffice it to say that a refer-ence type contains a reference to a location in memory that is differentfrom where the actual data resides. Code that sets a variable to null explic-itly assigns the reference to point at nothing. In fact, it is even possible tocheck whether a reference type points to nothing. Listing 2.16 demon-strates assigning null to a string variable.Listing 2.16: Assigning null to a String static void Main() { string faxNumber; From the Library of Wow! eBook52 C hapter 2: Data Types // ... // Clear the value of faxNumber. faxNumber = null; // ... } It is important to note that assigning the value null to a reference type is distinct from not assigning it at all. In other words, a variable that has been assigned null has still been set, and a variable with no assignment has not been set and therefore will often cause a compile error if used prior to assignment. Assigning the value null to a string is distinctly different from assign- ing an empty string, . null indicates that the variable has no value. indicates that there is a value: an empty string. This type of distinction can be quite useful. For example, the programming logic could interpret a faxNumber of null to mean that the fax number is unknown, while a faxNumber value of could indicate that there is no fax number. The void Nontype Sometimes the C# syntax requires a data type to be specified but no data is passed. For example, if no return from a method is needed C# allows the use of void to be specified as the data type instead. The declaration of Main within the HelloWorld program is an example. Under these circumstances, the data type to specify is void. The use of void as the return type indicates that the method is not returning any data and tells the compiler not to expect a value. void is not a data type per se, but rather an identification of the fact that there is no data type. Language Contrast: C++—void Is a Data Type In C++, void is a data type commonly used as void**. In C#, void is not considered a data type in the same way. Rather, it is used to identify that a method does not return a value. From the Library of Wow! eBook 53 null a nd void Language Contrast: Visual Basic—Returning void Is Like Defining a Subroutine The Visual Basic equivalent of returning a void in C# is to define a subrou- tine (Sub/End Sub) rather than a function that returns a value. ADVANCED TOPICImplicitly Typed Local VariablesAdditionally, C# 3.0 includes a contextual keyword, var, for declaring animplicitly typed local variable. As long as the code initializes a variable atdeclaration time with an unambiguous type, C# 3.0 allows for the variabledata type to be implied. Instead of explicitly specifying the data type, animplicitly typed local variable is declared with the contextual keywordvar, as shown in Listing 2.17.Listing 2.17: Working with Strings class Uppercase { static void Main() { Syst ...
Nội dung trích xuất từ tài liệu:
Addison essential C# 4.0 Visual Studio_2 51 null a nd void To actually change the value in text, assign the value from ToUpper()back into text, as in the following: text = text.ToUpper();System.Text.StringBuilderIf considerable string modification is needed, such as when constructing along string in multiple steps, you should use the data type System.Text.StringBuilder rather than string. System.Text.StringBuilderincludes methods such as Append(), AppendFormat(), Insert(), Remove(),and Replace(), some of which also appear on string. The key difference,however, is that on System.Text.StringBuilder these methods willmodify the data in the StringBuilder itself, and will not simply return anew string.null and voidTwo additional keywords relating to types are null and void. null is avalue which indicates that the variable does not refer to any valid object.void is used to indicate the absence of a type or the absence of any valuealtogether.nullnull can also be used as a type of string “literal.” null indicates that a vari-able is set to nothing. Reference types, pointer types, and nullable valuetypes can be assigned the value null. The only reference type covered sofar in this book is string; Chapter 5 covers the topic of creating classes(which are reference types) in detail. For now, suffice it to say that a refer-ence type contains a reference to a location in memory that is differentfrom where the actual data resides. Code that sets a variable to null explic-itly assigns the reference to point at nothing. In fact, it is even possible tocheck whether a reference type points to nothing. Listing 2.16 demon-strates assigning null to a string variable.Listing 2.16: Assigning null to a String static void Main() { string faxNumber; From the Library of Wow! eBook52 C hapter 2: Data Types // ... // Clear the value of faxNumber. faxNumber = null; // ... } It is important to note that assigning the value null to a reference type is distinct from not assigning it at all. In other words, a variable that has been assigned null has still been set, and a variable with no assignment has not been set and therefore will often cause a compile error if used prior to assignment. Assigning the value null to a string is distinctly different from assign- ing an empty string, . null indicates that the variable has no value. indicates that there is a value: an empty string. This type of distinction can be quite useful. For example, the programming logic could interpret a faxNumber of null to mean that the fax number is unknown, while a faxNumber value of could indicate that there is no fax number. The void Nontype Sometimes the C# syntax requires a data type to be specified but no data is passed. For example, if no return from a method is needed C# allows the use of void to be specified as the data type instead. The declaration of Main within the HelloWorld program is an example. Under these circumstances, the data type to specify is void. The use of void as the return type indicates that the method is not returning any data and tells the compiler not to expect a value. void is not a data type per se, but rather an identification of the fact that there is no data type. Language Contrast: C++—void Is a Data Type In C++, void is a data type commonly used as void**. In C#, void is not considered a data type in the same way. Rather, it is used to identify that a method does not return a value. From the Library of Wow! eBook 53 null a nd void Language Contrast: Visual Basic—Returning void Is Like Defining a Subroutine The Visual Basic equivalent of returning a void in C# is to define a subrou- tine (Sub/End Sub) rather than a function that returns a value. ADVANCED TOPICImplicitly Typed Local VariablesAdditionally, C# 3.0 includes a contextual keyword, var, for declaring animplicitly typed local variable. As long as the code initializes a variable atdeclaration time with an unambiguous type, C# 3.0 allows for the variabledata type to be implied. Instead of explicitly specifying the data type, animplicitly typed local variable is declared with the contextual keywordvar, as shown in Listing 2.17.Listing 2.17: Working with Strings class Uppercase { static void Main() { Syst ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính tài liệu công nghệ thông tin lập trình máy tính mẹo máy tính cài đặt máy tínhGợi ý tài liệu liên quan:
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 315 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 305 0 0 -
Thêm chức năng hữu dụng cho menu chuột phải trên Windows
4 trang 288 0 0 -
70 trang 251 1 0
-
Bài giảng Tin học lớp 11 bài 1: Giới thiệu ngôn ngữ lập trình C#
15 trang 237 0 0 -
Tổng hợp lỗi Win 8 và cách sửa
3 trang 232 0 0 -
Sửa lỗi các chức năng quan trọng của Win với ReEnable 2.0 Portable Edition
5 trang 213 0 0 -
Giáo trình Bảo trì hệ thống và cài đặt phần mềm
68 trang 207 0 0 -
UltraISO chương trình ghi đĩa, tạo ổ đĩa ảo nhỏ gọn
10 trang 203 0 0 -
Phần III: Xử lý sự cố Màn hình xanh
3 trang 203 0 0