Professional C# 6 and .NET Core 1.0. Christian Nagel

Читать онлайн.
Название Professional C# 6 and .NET Core 1.0
Автор произведения Christian Nagel
Жанр Зарубежная образовательная литература
Серия
Издательство Зарубежная образовательная литература
Год выпуска 0
isbn 9781119096634



Скачать книгу

type system, based on a distinction between value and reference types. Chapters 3 and 4 cover the C# object-oriented programming features.

      3

      Objects and Types

      What’s In This Chapter?

      • The differences between classes and structs

      • Class members

      • Expression-bodied members

      • Passing values by value and by reference

      • Method overloading

      • Constructors and static constructors

      • Read-only fields

      • Enumerations

      • Partial classes

      • Static classes

      • The Object class, from which all other types are derived

      Wrox.com Code Downloads for This Chapter

      The wrox.com code downloads for this chapter are found at www.wrox.com/go/professionalcsharp6 on the Download Code tab. The code for this chapter is divided into the following major examples:

      • MathSample

      • MethodSample

      • StaticConstructorSample

      • StructsSample

      • PassingByValueAndByReference

      • OutKeywordSample

      • EnumSample

      • ExtensionMethods

Creating and Using Classes

      So far, you’ve been introduced to some of the building blocks of the C# language, including variables, data types, and program flow statements, and you have seen a few very short complete programs containing little more than the Main method. What you haven’t seen yet is how to put all these elements together to form a longer, complete program. The key to this lies in working with classes – the subject of this chapter. Note Chapter 4, “Inheritance,” covers inheritance and features related to inheritance.

      NOTE This chapter introduces the basic syntax associated with classes. However, we assume that you are already familiar with the underlying principles of using classes – for example, that you know what a constructor or a property is. This chapter is largely confined to applying those principles in C# code.

Classes and Structs

      Classes and structs are essentially templates from which you can create objects. Each object contains data and has methods to manipulate and access that data. The class defines what data and behavior each particular object (called an instance) of that class can contain. For example, if you have a class that represents a customer, it might define fields such as CustomerID, FirstName, LastName, and Address, which are used to hold information about a particular customer. It might also define functionality that acts upon the data stored in these fields. You can then instantiate an object of this class to represent one specific customer, set the field values for that instance, and use its functionality:

      Structs differ from classes because they do not need to be allocated on the heap (classes are reference types and are always allocated on the heap). Structs are value types and are usually stored on the stack. Also, structs cannot derive from a base struct.

      You typically use structs for smaller data types for performance reasons. In terms of syntax, however, structs look very similar to classes; the main difference is that you use the keyword struct instead of class to declare them. For example, if you wanted all PhoneCustomer instances to be allocated on the stack instead of the managed heap, you could write the following:

      For both classes and structs, you use the keyword new to declare an instance. This keyword creates the object and initializes it; in the following example, the default behavior is to zero out its fields:

      In most cases, you use classes much more often than structs. Therefore, this chapter covers classes first and then the differences between classes and structs and the specific reasons why you might choose to use a struct instead of a class. Unless otherwise stated, however, you can assume that code presented for a class works equally well for a struct.

      NOTE An important difference between classes and structs is that objects of type of class are passed by reference, and objects of type of a struct are passed by value. This is explained later in this chapter in the section “Passing Parameters by Value and by Reference.”

Classes

      A class contains members, which can be static or instance members. A static member belongs to the class; an instance member belongs to the object. With static fields, the value of the field is the same for every object. With instance fields, every object can have a different value. Static members have the static modifier attached.

      The kind of members are explained in the following table.

      Let’s get into the details of class members.

      Fields

      Fields are any variables associated with the class. You have already seen fields in use in the PhoneCustomer class in the previous example.

      After you have instantiated a PhoneCustomer object, you can then access these fields using the object.FieldName syntax, as shown in this example:

      Constants can be associated with classes in the same way as variables. You declare a constant using the const keyword. If it is declared as public, then it is accessible from outside the class:

      It’s a good idea not to declare fields public. If you change a public member of a class, every caller that’s using this public member needs to be changed as well. For example, in case you want to introduce a check for the maximum string length with the next version, the public field needs to be changed to a property. Existing code that makes use of the public field must be recompiled for using this property (although the syntax from the caller side looks the same with properties). If instead you just change the check within an existing property, the caller doesn’t need to be recompiled for using the new version.

      It’s good practice to declare fields private and use properties to access the field, as described in the next section.

      Properties

      The idea of a property is that it is a method or a pair of methods dressed to look like a field. Let’s change the field for the first name from the previous example to a private field with the variable name _firstName. The property named FirstName contains a get and set accessor to retrieve and set the value of the backing field:

      The get accessor takes no parameters and must return the same type as the declared property. You should not specify any explicit parameters for the set accessor either, but the compiler assumes it takes one parameter, which is of the same type again, and which is referred to as value.

      Let’s get into another example with a different naming convention. The following code contains a property called Age, which sets a field called age. In this example, age is referred to as the backing variable for the property Age:

      Note the naming convention used here. You take advantage of C#’s case sensitivity by using the same name – Pascal-case for the public property, and camel-case for the equivalent private field if there is one. In earlier .NET versions, this naming convention was preferred by Microsoft’s C# team. Recently they switched to the naming convention