value types versus reference types
DESCRIPTION
Value Types Versus Reference Types. All C# types fall into the following categories: Value types Reference types Generic type parameters Pointer types. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/1.jpg)
VALUE TYPES VERSUS REFERENCE TYPES
• All C# types fall into the following categories:• Value types• Reference types• Generic type parameters• Pointer types
![Page 2: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/2.jpg)
• 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.
![Page 3: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/3.jpg)
VALUE TYPES
![Page 4: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/4.jpg)
![Page 5: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/5.jpg)
REFERENCE TYPE
![Page 6: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/6.jpg)
![Page 7: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/7.jpg)
ARRAYS
![Page 8: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/8.jpg)
• An array is a set of uniform data elements, represented by a single variable name
![Page 9: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/9.jpg)
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.
![Page 10: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/10.jpg)
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.
![Page 11: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/11.jpg)
![Page 12: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/12.jpg)
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 dimensionsint x = myArray2[4, 6, 1] // One set of square brackets
• Jagged arrays • Are multidimensional arrays where each subarray is 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
![Page 13: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/13.jpg)
![Page 14: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/14.jpg)
• 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
![Page 15: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/15.jpg)
• Although an array is always a reference type, the elements of the array can be either value types or reference types.
![Page 16: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/16.jpg)
DECLARATION OF ARRAYS
![Page 17: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/17.jpg)
![Page 18: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/18.jpg)
ACCESSING ARRAYS
![Page 19: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/19.jpg)
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} } ;
![Page 20: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/20.jpg)
![Page 21: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/21.jpg)
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];
![Page 22: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/22.jpg)
• 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 };
![Page 23: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/23.jpg)
DELEGATES
![Page 24: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/24.jpg)
• 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:int answer = t(3); // answer is 9
![Page 25: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/25.jpg)
delegate int Transformer (int x);class Test{static void Main(){Transformer t = Square;// Create delegate instanceint result = t(3);// Invoke delegateConsole.WriteLine (result); // 9119}static int Square (int x) { return x * x; }}
![Page 26: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/26.jpg)
EVENTS
![Page 27: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/27.jpg)
• 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 struct as the event or in a different class or struct.
![Page 28: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/28.jpg)
![Page 29: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/29.jpg)
DECLARING AN EVENT
• One event
![Page 30: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/30.jpg)
• More than one event
![Page 31: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/31.jpg)
• Static events
![Page 32: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/32.jpg)
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.
![Page 33: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/33.jpg)
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.
![Page 34: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/34.jpg)
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.
![Page 35: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/35.jpg)
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
![Page 36: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/36.jpg)
• Method form and static method
![Page 37: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/37.jpg)
• Anonymous and lambda methods
![Page 38: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/38.jpg)
![Page 39: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/39.jpg)
INTERFACES
![Page 40: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/40.jpg)
• 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).
![Page 41: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/41.jpg)
Interface members are always implicitly public and cannot declare an access modifier. Implementing an interface means providing a public implementation for all itsmembers:
![Page 42: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/42.jpg)
GENERICS
![Page 43: Value Types Versus Reference Types](https://reader035.vdocument.in/reader035/viewer/2022062323/5681609d550346895dcfc5af/html5/thumbnails/43.jpg)
• A delegate type may contain generic type parameters.