Home > Articles > Programming > C#

This chapter is from the book

Summary

Section 4.2 Classes, Objects, Methods, Properties and Instance Variables

  • Methods perform tasks. Each method describes the mechanisms that actually perform its tasks. The method hides from its user the complex tasks that it performs.
  • The application unit that houses a method is called a class. A class may contain one or more methods that are designed to perform the class's tasks.
  • A method can perform a task and may return a result.
  • An instance of a class is called an object.
  • Each message sent to an object is a method call and tells that method to perform its task.
  • Each method can specify parameters that represent additional information the method requires to perform its task correctly. A method call supplies arguments for the method's parameters.
  • An object has attributes that are carried with the object as it's used in an application. These attributes are specified as part of the object's class. Attributes are specified in classes by fields.
  • An object has properties for accessing attributes. Properties contain get accessors for reading attributes and set accessors for storing into them.

Section 4.3 Declaring a Class with a Method and Instantiating an Object of a Class

  • Keyword public is an access modifier.
  • Every class declaration contains keyword class followed immediately by the class's name.
  • A method declaration that begins with keyword public indicates that the method is "available to the public"—that is, it can be called by other classes declared outside the class declaration.
  • Keyword void indicates that a method will not return any information when it completes its task.
  • By convention, method names begin with an uppercase first letter, and all subsequent words in the name begin with an uppercase first letter. This is called Pascal case.
  • Empty parentheses following a method name indicate that the method does not require any parameters to perform its task.
  • Every method's body is delimited by left and right braces ({ and }).
  • The body of a method contains statements that perform the method's task. After the statements execute, the method has completed its task.
  • When you attempt to execute an application, C# looks for a Main method to begin execution.
  • Typically, you create an object of a class to call the class's methods.
  • Object creation expressions begin with the new operator and create new objects.
  • To call a method of an object, follow the variable name with a member access operator (.), the method name and a set of parentheses containing the method's arguments.
  • In the UML, each class is modeled in a class diagram as a rectangle with three compartments. The top compartment contains the name of the class, centered horizontally in boldface. The middle compartment contains the class's attributes, which correspond to fields in C#. The bottom compartment contains the class's operations, which correspond to methods and constructors in C#.
  • The UML models operations by listing the operation name, followed by a set of parentheses. A plus sign (+) in front of the operation name indicates that the operation is a public operation in the UML (i.e., a public method in C#). The plus sign is called the public visibility symbol.

Section 4.4 Declaring a Method with a Parameter

  • Methods often require additional information to perform their tasks. Such additional information is provided to methods via arguments in method calls.
  • Console method ReadLine reads characters until a newline character is encountered, then returns the characters as a string.
  • A method that requires data to perform its task must specify this in its declaration by placing additional information in the method's parameter list.
  • Each parameter must specify both a type and an identifier.
  • At the time a method is called, its arguments are assigned to its parameters. Then the method body uses the parameter variables to access the argument values.
  • A method can specify multiple parameters in a comma-separated parameter list.
  • The number of arguments in the method call must match the number of required parameters in the method declaration's parameter list. Also, the argument types in the method call must be consistent with the types of the corresponding parameters in the method's declaration.
  • The UML models a parameter of an operation by listing the parameter name, followed by a colon and the parameter type between the parentheses following the operation name.
  • The UML does not provide types that correspond to every C# type. For this reason, and to avoid confusion between UML types and C# types, we use only C# types in our UML diagrams.
  • There's a special relationship between classes that are compiled in the same project. By default, such classes are considered to be in the same namespace. A using directive is not required when one class in a namespace uses another in the same namespace.
  • A using directive is not required if you always refer to a class with its fully qualified class name.

Section 4.5 Instance Variables and Properties

  • Local variables can be used only in the method in which they're declared.
  • A class normally contains methods that manipulate the attributes that belong to a particular object of the class. Attributes are represented as instance variables in a class declaration. Such variables are declared inside a class declaration but outside its method's bodies.
  • Each object (instance) of a class has a separate copy of each instance variable.
  • Most instance-variable declarations are preceded with the private access modifier. Variables, properties or methods declared with access modifier private are accessible only to methods (and properties) of the class in which they're declared.
  • Declaring instance variables with access modifier private is known as information hiding.
  • Properties contain accessors that handle the details of modifying and returning data.
  • Properties provide a controlled way for programmers to "get" (i.e., retrieve) the value in an instance variable and "set" (i.e., modify) the value in an instance variable.
  • A property declaration can contain a get accessor, a set accessor or both. The get accessor typically enables a client to read the value of a private instance variable. The set accessor typically enables a client to modify that instance variable's value.
  • After defining a property, you can use it the same way as you use a variable.
  • The default value for a field of type string is null.

Section 4.6 UML Class Diagram with a Property

  • We model properties in the UML as attributes, preceded by the word "property" in guillemets (« and µ). Using descriptive words in guillemets (called stereotypes in the UML) helps distinguish properties from other attributes.
  • A class diagram helps you design a class, so it's not required to show every implementation detail of the class. Since an instance variable that's manipulated by a property is really an implementation detail of that property, our class diagrams do not show instance variables.
  • private class members are preceded by the private visibility symbol (-) in the UML.
  • The UML represents instance variables and properties as attributes by listing the attribute name, followed by a colon and the attribute type.

Section 4.7 Software Engineering with Properties and set and get Accessors

  • Properties can scrutinize attempts to modify an instance variable's value (known as data validation), thus ensuring that the new value for that instance variable is valid.
  • Using properties would seem to violate the notion of private data. However, a set accessor can provide data-validation capabilities to ensure that the value is set properly; get and set accessors can translate between the format of the data used by the client and the format used in the private instance variable.
  • A benefit of fields over local variables is that all of a class's methods and properties can use the fields. Another distinction is that a field has a default initial value provided by C# when you do not specify the field's initial value, but a local variable does not.

Section 4.8 Auto-Implemented Properties

  • With an auto-implemented property, the C# compiler automatically creates a private instance variable, and the get and set accessors for returning and modifying the private instance variable.
  • Visual C# 2010 Express and Visual Studio 2010 have a feature called code snippets that allows you to insert predefined code templates into your source code. One such snippet enables you to insert a public auto-implemented property by typing the word "prop" in the code window and pressing the Tab key twice.
  • Pieces of the inserted code are highlighted for you to easily change the property's type and name. Press the Tab key to move from one highlighted piece of text to the next in the inserted code.
  • To get a list of all available code snippets, type Ctrl + k, Ctrl + x. This displays the Insert Snippet window in the code editor. This feature can also be accessed by right clicking in the source code editor and selecting the Insert Snippet... menu item.

Section 4.9 Value Types vs. Reference Types

  • Types are divided into two categories—value types and reference types.
  • A variable of a value type contains data of that type.
  • A variable of a reference type (sometimes called a reference) contains the address of a location in memory where an object is stored.
  • Reference-type instance variables are initialized by default to the value null.

Section 4.10 Initializing Objects with Constructors

  • A constructor can be used to initialize an object of a class when the object is created.
  • If no constructor is provided for a class, the compiler provides a public default constructor with no parameters that does not modify the instance variables' default values.
  • Like operations, the UML models constructors in the third compartment of a class diagram. To distinguish a constructor from a class's operations, the UML places the word "constructor" between guillemets (« and µ) before the constructor's name.
  • Constructors can specify parameters but cannot specify return types.

Section 4.11 Floating-Point Numbers and Type decimal

  • A real number is a number with a decimal point, such as 7.33, 0.0975 or 1000.12345. C# provides three simple types for storing real numbers—float, double, and decimal.
  • Types float and double are called floating-point types. The primary difference between them and the decimal type is that decimal variables store a limited range of real numbers precisely, but floating-point variables store approximations of real numbers across a much greater range.
  • Variables of type float represent single-precision floating-point numbers and have seven significant digits. Variables of type double represent double-precision floating-point numbers. These require twice as much storage as float variables and provide 15–16 significant digits—approximately double the precision of float variables. Furthermore, variables of type decimal require twice as much storage as double variables and provide 28–29 significant digits.
  • Real number values that appear in source code are of type double by default.
  • Convert method ToDecimal extracts a decimal value from a string.
  • The : in a format item indicates that the next character represents a format specifier.
  • The C format specifier specifies a monetary amount (C is for currency).
  • It's possible to declare the get and set accessors of a property with different access modifiers. One accessor must implicitly have the same access as the property and the other must be declared with a more restrictive access modifier than the property; private is more restrictive than public.
  • + Share This
  • 🔖 Save To Your Account