Addison Essential Csharp_3
Số trang: 98
Loại file: pdf
Dung lượng: 1.87 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:
Khi đầu ra 5,8 cho thấy, các tên sở hữu kết quả được xác định bởi trình biên dịch để phù hợp với tài sản từ nơi mà giá trị được lấy. Mặc dù trình biên dịch cho phép các tờ khai loại vô danh như những người bạn được hiển thị trong Liệt kê 5,33, nói chung bạn nên tránh các tờ khai loại vô danh và thậm chí gõ tiềm ẩn liên quan
Nội dung trích xuất từ tài liệu:
Addison Essential Csharp_3 247 S tatic Membersresult in compile errors. Even IntelliSense in IDEs such as Visual Studio2008 works with the anonymous type. In Listing 5.33, member names on the anonymous types are explicitlyidentified using the assignment of the value to the name (see Title andYearOfPublication in patent1 and patent2 assignments). However, if thevalue assigned is a property or field, the name will default to the name ofthe field or property if not specified explicitly. patent3, for example, isdefined using a property name “Title” rather than an assignment to animplicit name. As Output 5.8 shows, the resultant property name is deter-mined by the compiler to match the property from where the value wasretrieved. Although the compiler allows anonymous type declarations such as theones shown in Listing 5.33, you should generally avoid anonymous typedeclarations and even the associated implicit typing with var until you areworking with lambda and query expressions that associate data from dif-ferent types or you are horizontally projecting the data so that for a partic-ular type, there is less data overall. Until frequent querying of data out ofcollections makes explicit type declaration burdensome, it is preferable toexplicitly declare types as outlined in this chapter.Static MembersThe HelloWorld example in Chapter 1 first presented the keyword static;however, it did not define it fully. This section defines the static keywordfully. To begin, consider an example. Assume that the employee Id valueneeds to be unique for each employee. One way to accomplish this is tostore a counter to track each employee ID. If the value is stored as aninstance field, however, every time you instantiate an object, a new NextIdfield will be created such that every instance of the Employee object wouldconsume memory for that field. The biggest problem is that each time anEmployee object instantiated, the NextId value on all of the previouslyinstantiated Employee objects would need to be updated with the next IDvalue. What you need is a single field that all Employee object instancesshare. From the Library of Wow! eBook248 C hapter 5: Classes Language Contrast: C++/Visual Basic—Global Variables and Functions Unlike many of the languages that came before it, C# does not have global variables or global functions. All fields and methods in C# appear within the context of a class. The equivalent of a global field or function within the realm of C# is a static field or function. There is no functional difference between global variables/functions and C# static fields/methods, except that static fields/methods can include access modifiers, such as private, that can limit the access and provide better encapsulation. Static Fields To define data that is available across multiple instances, you use the static keyword, as demonstrated in Listing 5.34. Listing 5.34: Declaring a Static Field class Employee { public Employee(string firstName, string lastName) { FirstName = firstName; LastName = lastName; Id = NextId; NextId++; } // ... public static int NextId; public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Salary { get; set; } // ... } In this example, the NextId field declaration includes the static modifier and therefore is called a static field. Unlike Id, a single storage location for NextId is shared across all instances of Employee. Inside the Employee con- structor, you assign the new Employee object’s Id the value of NextId From the Library of Wow! eBook 249 S tatic Membersimmediately before incrementing it. When another Employee class iscreated, NextId will be incremented and the new Employee object’s Id fieldwill hold a different value. Just as instance fields (nonstatic fields) can be initialized at declarationtime, so can static fields, as demonstrated in Listing 5.35.Listing 5.35: Assigning a Static Field at Declaration class Employee { // ... public static int NextId = 42; // ... }Unlike with instance fields, if no initialization for a ...
Nội dung trích xuất từ tài liệu:
Addison Essential Csharp_3 247 S tatic Membersresult in compile errors. Even IntelliSense in IDEs such as Visual Studio2008 works with the anonymous type. In Listing 5.33, member names on the anonymous types are explicitlyidentified using the assignment of the value to the name (see Title andYearOfPublication in patent1 and patent2 assignments). However, if thevalue assigned is a property or field, the name will default to the name ofthe field or property if not specified explicitly. patent3, for example, isdefined using a property name “Title” rather than an assignment to animplicit name. As Output 5.8 shows, the resultant property name is deter-mined by the compiler to match the property from where the value wasretrieved. Although the compiler allows anonymous type declarations such as theones shown in Listing 5.33, you should generally avoid anonymous typedeclarations and even the associated implicit typing with var until you areworking with lambda and query expressions that associate data from dif-ferent types or you are horizontally projecting the data so that for a partic-ular type, there is less data overall. Until frequent querying of data out ofcollections makes explicit type declaration burdensome, it is preferable toexplicitly declare types as outlined in this chapter.Static MembersThe HelloWorld example in Chapter 1 first presented the keyword static;however, it did not define it fully. This section defines the static keywordfully. To begin, consider an example. Assume that the employee Id valueneeds to be unique for each employee. One way to accomplish this is tostore a counter to track each employee ID. If the value is stored as aninstance field, however, every time you instantiate an object, a new NextIdfield will be created such that every instance of the Employee object wouldconsume memory for that field. The biggest problem is that each time anEmployee object instantiated, the NextId value on all of the previouslyinstantiated Employee objects would need to be updated with the next IDvalue. What you need is a single field that all Employee object instancesshare. From the Library of Wow! eBook248 C hapter 5: Classes Language Contrast: C++/Visual Basic—Global Variables and Functions Unlike many of the languages that came before it, C# does not have global variables or global functions. All fields and methods in C# appear within the context of a class. The equivalent of a global field or function within the realm of C# is a static field or function. There is no functional difference between global variables/functions and C# static fields/methods, except that static fields/methods can include access modifiers, such as private, that can limit the access and provide better encapsulation. Static Fields To define data that is available across multiple instances, you use the static keyword, as demonstrated in Listing 5.34. Listing 5.34: Declaring a Static Field class Employee { public Employee(string firstName, string lastName) { FirstName = firstName; LastName = lastName; Id = NextId; NextId++; } // ... public static int NextId; public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Salary { get; set; } // ... } In this example, the NextId field declaration includes the static modifier and therefore is called a static field. Unlike Id, a single storage location for NextId is shared across all instances of Employee. Inside the Employee con- structor, you assign the new Employee object’s Id the value of NextId From the Library of Wow! eBook 249 S tatic Membersimmediately before incrementing it. When another Employee class iscreated, NextId will be incremented and the new Employee object’s Id fieldwill hold a different value. Just as instance fields (nonstatic fields) can be initialized at declarationtime, so can static fields, as demonstrated in Listing 5.35.Listing 5.35: Assigning a Static Field at Declaration class Employee { // ... public static int NextId = 42; // ... }Unlike with instance fields, if no initialization for a ...
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 318 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 291 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 238 0 0 -
Tổng hợp lỗi Win 8 và cách sửa
3 trang 234 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 214 0 0 -
Giáo trình Bảo trì hệ thống và cài đặt phần mềm
68 trang 208 0 0 -
Tổng hợp 30 lỗi thương gặp cho những bạn mới sử dụng máy tính
9 trang 205 0 0 -
Phần III: Xử lý sự cố Màn hình xanh
3 trang 204 0 0