430 likes | 673 Views
Value Types Versus Reference Types. All C# types fall into the following categories: Value types Reference types Generic type parameters Pointer types.
E N D
Value Types Versus Reference Types • All C# types fall into the following categories: • Value types • Reference types • Generic type parameters • Pointer types
Value types comprise most built-in types (specifically, all numeric types, the char type, and the bool type) as well as custom struct and enum types. • Reference types comprise all class, array, delegate, and interface types. (This includes the predefined string type.) • The fundamental difference between value types and reference types is how they are handled in memory.
An array is a set of uniform data elements, represented by a single variable name
Definitions • Elements: The individual data items of an array are called elements. All elements of an array must be of the same type or derived from the same type. • Rank/dimensions: Arrays can have any positive number of dimensions. The number of dimensions an array has is called its rank. • Dimension length: Each dimension of an array has a length, which is the number of positions in that direction. • Array length: The total number of elements contained in an array, in all dimensions, is called the length of the array.
Note • Once an array is created, its size is fixed. C# does not support dynamic arrays. • Array indexes are 0-based. That is, if the length of a dimension is n, the index values range from 0 to n – 1. • Notice that for each dimension, the indexes range from 0 to length – 1.
Types of arrays • 1-D • 2-D • Rectangular arrays • Are multidimensional arrays where all the subarrays in a particular dimension have the same length • Always use a single set of square brackets, regardless of the number of dimensions intx = myArray2[4, 6, 1] // One set of square brackets • Jagged arrays • Are multidimensional arrays where each subarrayis an independent array • Can have subarrays of different lengths • Use a separate set of square brackets for each dimension of the array jagArray1[2][7][4] // Three sets of square brackets
An array instance is an object whose type derives from class System.Array. • Arrays are reference types, and as with all reference types, they have both a reference to the data and the data object itself. The reference is in either the stack or the heap, and the data object itself will always be in the heap
Although an array is always a reference type, the elements of the array can be either value types or reference types.
Initializing • Value 0 • int[] intArr = new int[4]; • 1-D • int[] intArr = new int[] { 10, 20, 30, 40 }; • 2-D • int[,] intArray2 = new int[,] { {10, 1}, {2, 10}, {11, 9} } ;
Jagged Arrays • The following is a declaration of a single-dimensional array that has three elements, each of which is a single-dimensional array of integers: • int[][] jaggedArray = new int[3][]; • Each of the elements is a single-dimensional array of integers. The first element is an array of 5 integers, the second is an array of 4 integers, and the third is an array of 2 integers. • jaggedArray[0] = new int[5]; • jaggedArray[1] = new int[4]; • jaggedArray[2] = new int[2];
It is also possible to use initializers to fill the array elements with values, in which case you do not need the array size • jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };jaggedArray[1] = new int[] { 0, 2, 4, 6 };jaggedArray[2] = new int[] { 11, 22 };
A delegate is an object that knows how to call a method. • A delegate type defines the kind of method that delegate instances can call. delegate int Transformer (int x); • Transformer is compatible with any method with an int return type and a single int parameter, such as this: static int Square (int x) { return x * x; } • Assigning a method to a delegate variable creates a delegate instance: Transformer t = Square; • which can be invoked in the same way as a method: intanswer = t(3); // answer is 9
delegate int Transformer (int x); class Test { static void Main() { Transformer t = Square;// Create delegate instance int result = t(3);// Invoke delegate Console.WriteLine (result); // 9 119 } static int Square (int x) { return x * x; } }
Events are like delegates • Raising an event: The term for invoking or firing an event. When an event is raised, all the methods registered with it are invoked—in order. • Publisher: A class or struct that makes an event available to other classes or structs for their use. • Subscriber: A class or struct that registers methods with a publisher. • Event handler: A method that is registered with an event. It can be declared in the same class or structas the event or in a different class or struct.
Declaring an event • One event
An event is a member • A common error is to think of an event as a type, which it is not. An event is a member, and there are several important ramifications to this: • Because a member is not a type, you do not use an object-creation expression (a new expression) to create its object. • Because an event is a member • It must be declared in a class or struct, with the other members. • You cannot declare an event in a block of executable code. • An event member is implicitly and automatically initialized to null with the other members.
The delegate type and eventhandler • An event declaration requires the name of a delegate type. You can either declare one or use one that already exists. If you declare a delegate type, it must specify the signature and return type of the methods that will be stored by the event.
Raising an event • The event member itself just holds the event handlers that need to be invoked. • Nothing happens with them unless the event is raised. • You need to make sure there is code to do just that, at the appropriate times.
Subscribing to an event • To add an event handler to an event, the handler musthave the same return type and signature as the • event’s delegate. • Use the += operator to add an event handler toan event, as shown in the following code. • The method can be any of the following: • An instance method • A static method • An anonymous method • A lambda expression
An interface is similar to a class, but it provides a specification rather than an implementation for its members. An interface is special in the following ways: • Interface members are all implicitly abstract. In contrast, a class can provideboth abstract members and concrete members with implementations. • A class (or struct) can implement multiple interfaces. In contrast, a class can inherit from only a single class, and a struct cannot inherit at all (aside from deriving from System.ValueType).
Interface members are always implicitly public and cannot declare an access modifier. Implementing an interface means providing a public implementation for all its members: