Skip to main content

Posts

Showing posts from December, 2014

Properties in C#

Properties are a natural extension of fields. Both are named members with associated types, and the syntax for accessing fields and properties is the same. However, unlike fields, properties do not denote storage locations. Instead, properties have accessors that specify the statements to be executed when their values are read or written. A property is declared like a field, except that the declaration ends with a get accessor and/or a set accessor written between the delimiters { and } instead of ending in a semicolon. A property that has both a get accessor and a set accessor is a read-write property , a property that has only a get accessor is a read-only property , and a property that has only a set accessor is a write-only property . A get accessor corresponds to a parameterless method with a return value of the property type. Except as the target of an assignment, when a property is referenced in an expression, the get accessor of the property is invoked to

Constructors in C#

C# supports both instance and static constructors. An instance constructor is a member that implements the actions required to initialize an instance of a class. A static constructor is a member that implements the actions required to initialize a class itself when it is first loaded. A constructor is declared like a method with no return type and the same name as the containing class. If a constructor declaration includes a static modifier, it declares a static constructor. Otherwise, it declares an instance constructor. Instance constructors can be overloaded. For example, the List<T> class declares two instance constructors, one with no parameters and one that takes an int parameter. Instance constructors are invoked using the new operator. The following statements allocate two List<string> instances using each of the constructors of the List class. List<string> list1 = new List<string>(); List<string> list2 = new List<strin

Method overloading (C# Specification Part 9)

Method overloading permits multiple methods in the same class to have the same name as long as they have unique signatures. When compiling an invocation of an overloaded method, the compiler uses overload resolution to determine the specific method to invoke. Overload resolution finds the one method that best matches the arguments or reports an error if no single best match can be found. The following example shows overload resolution in effect. The comment for each invocation in the Main method shows which method is actually invoked. class Test {       static void F() {             Console.WriteLine("F()");       }       static void F(object x) {             Console.WriteLine("F(object)");       }       static void F(int x) {             Console.WriteLine("F(int)");       }       static void F(double x) {             Console.WriteLine("F(double)");       }       static void F<T>(T x) {             Console.WriteL

Virtual, Override, and Abstract methods ( C# Specification Part 8)

When an instance method declaration includes a virtual modifier, the method is said to be a virtual method . When no virtual modifier is present, the method is said to be a non-virtual method . When a virtual method is invoked, the run-time type of the instance for which that invocation takes place determines the actual method implementation to invoke. In a nonvirtual method invocation, the compile-time type of the instance is the determining factor. A virtual method can be overridden in a derived class. When an instance method declaration includes an override modifier, the method overrides an inherited virtual method with the same signature. Whereas a virtual method declaration introduces a new method, an override method declaration specializes an existing inherited virtual method by providing a new implementation of that method. An abstract method is a virtual method with no implementation. An abstract method is declared with the abstract modifier and is permitt

Methods (C# Specification Part 7)

A method’s body specifies the statements to execute when the method is invoked. A method body can declare variables that are specific to the invocation of the method. Such variables are called local variables . A local variable declaration specifies a type name, a variable name, and possibly an initial value. The following example declares a local variable i with an initial value of zero and a local variable j with no initial value. using System; class Squares {       static void Main () {             int i = 0;             int j;             while (i < 10) {                   j = i * i;                   Console.WriteLine("{0} x {0} = {1}", i, j);                   i = i + 1;             }       } } C# requires a local variable to be definitely assigned before its value can be obtained. For example, if the declaration of the previous i did not include an initial value, the compiler would report an error for the subsequent usages of i because i w

Classes and objects (C# Specification Part 6)

Classes are the most fundamental of C#’s types. A class is a data structure that combines state (fields) and actions (methods and other function members) in a single unit. A class provides a definition for dynamically created instances of the class, also known as objects . Classes support inheritance and polymorphism , mechanisms whereby derived classes can extend and specialize base classes . New classes are created using class declarations. A class declaration starts with a header that specifies the attributes and modifiers of the class, the name of the class, the base class (if given), and the interfaces implemented by the class. The header is followed by the class body, which consists of a list of member declarations written between the delimiters { and } . The following is a declaration of a simple class named Point : public class Point {       public int x, y;       public Point(int x, int y) {             this.x = x;             this.y = y;       } }

C# Statements (C# Specification Part 4)

The actions of a program are expressed using statements . C# supports several different kinds of statements, a number of which are defined in terms of embedded statements. A block permits multiple statements to be written in contexts where a single statement is allowed. A block consists of a list of statements written between the delimiters { and } . Declaration statements are used to declare local variables and constants. Expression statements are used to evaluate expressions. Expressions that can be used as statements include method invocations, object allocations using the new operator, assignments using = and the compound assignment operators, increment and decrement operations using the ++ and -- operators and await expressions. Selection statements are used to select one of a number of possible statements for execution based on the value of some expression. In this group are the if and switch statements. Iteration statements are used to repeatedly execute