1 / 43

Value Types Versus Reference Types

Value Types Versus Reference Types. All C# types fall into the following categories: Value types Reference types Generic type parameters Pointer types.

aquene
Download Presentation

Value Types Versus Reference Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Value Types Versus Reference Types • All C# types fall into the following categories: • Value types • Reference types • Generic type parameters • Pointer types

  2. 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.

  3. Value types

  4. Reference type

  5. Arrays

  6. An array is a set of uniform data elements, represented by a single variable name

  7. 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.

  8. 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.

  9. 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

  10. 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

  11. Although an array is always a reference type, the elements of the array can be either value types or reference types.

  12. Declaration of arrays

  13. Accessing arrays

  14. 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} } ;

  15. 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];

  16. 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 };

  17. delegates

  18. 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

  19. 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; } }

  20. events

  21. 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.

  22. Declaring an event • One event

  23. More than one event

  24. Static events

  25. 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.

  26. 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.

  27. 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.

  28. 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

  29. Method form and static method

  30. Anonymous and lambda methods

  31. interfaces

  32. 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).

  33. Interface members are always implicitly public and cannot declare an access modifier. Implementing an interface means providing a public implementation for all its members:

  34. generics

  35. A delegate type may contain generic type parameters.

More Related