Chapter 5. Classes
You briefly saw in Chapter 1 how to declare a new class called HelloWorld. In Chapter 2, you learned about the built-in primitive types included with C#. Since you have now also learned about control flow and how to declar...
Defining and Instantiating a Class
Defining a class involves first specifying the keyword class, followed by an identifier, as shown in Listing 5.1.
Listing 5.1. Defining a Class
class Employee
{
}
All code that belongs to th...
Instance Fields
One of the key purposes of object-oriented design is the grouping of data to provide structure. This section discusses how to add data to the Employee class. The general object-oriented term for a variable that stores data within...
Instance Methods
One alternative to formatting the names in the WriteLine() method call within Main() is to provide a method on Employee that takes care of the formatting. Changing the functionality to be on Employee rather than on Program is cons...
Using the this Keyword
You can obtain the reference to a class from within instance members that belong to the class. To indicate explicitly that the field or method accessed is an instance member of the containing class in C#, you use the keyword...
Access Modifiers
When declaring a field earlier in the chapter, you prefixed the field declaration with the keyword public. public is an access modifier that identifies the level of encapsulation associated with the member it decorates. Five acces...
Constructors
Now that you have added fields to a class and can store data, you need to consider the validity of that data. As you saw in Listing 5.3, it is possible to instantiate an object using the new operator. The result, however, is the abili...
Static
The HelloWorld example in Chapter 1 first presented the keyword static; however, it did not define it fully. This section defines the static keyword fully.
Language Contrast: C++/Visual BasicGlobal Variables and Functions
Unlike man...
const and readonly Modifiers
Two more field modifiers are related to encapsulation. The first is the const modifier, which you already encountered when declaring local variables. The second is readonly.
const
Just as with const local variables...
Properties
The Access Modifiers section, earlier in this chapter, demonstrated how you can use the private keyword to encapsulate a password, preventing access from outside the class. This type of encapsulation is often too thorough, however. For ...
Nested Classes
In addition to defining methods and fields within a class, it is also possible to define a class within a class. Such classes are nested classes. You use a nested class when the class makes little sense outside the context of its co...
Partial Classes
Another language feature added in C# 2.0 is partial classes. Partial classes are portions of a class that the compiler can combine to form a complete class. Although you could define two or more partial classes within the same file...
Summary
This chapter explained C# constructs for classes and object orientation in C#. This included a discussion of fields, and a discussion of how to access them on a class instance.
Whether to store data on a per-instance basis or across all i...
С 2009 года мы стали переводить структура сайта на различные языки. Сайт теперь будет содержать книги не только на английском языке, но также и на других европейских языках, в том числе и на Русском языке.