43
VALUE TYPES VERSUS REFERENCE TYPES All C# types fall into the following categories: Value types Reference types Generic type parameters Pointer types

Value Types Versus Reference Types

  • Upload
    aquene

  • View
    65

  • Download
    0

Embed Size (px)

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 Presentation

Citation preview

Page 1: 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

Page 2: Value Types Versus Reference 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.

Page 3: Value Types Versus Reference Types

VALUE TYPES

Page 4: Value Types Versus Reference Types
Page 5: Value Types Versus Reference Types

REFERENCE TYPE

Page 6: Value Types Versus Reference Types
Page 7: Value Types Versus Reference Types

ARRAYS

Page 8: Value Types Versus Reference Types

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

Page 9: Value Types Versus Reference Types

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

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
Page 12: Value Types Versus Reference Types

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
Page 14: Value Types Versus Reference Types

• 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

• 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

DECLARATION OF ARRAYS

Page 17: Value Types Versus Reference Types
Page 18: Value Types Versus Reference Types

ACCESSING ARRAYS

Page 19: Value Types Versus 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} } ;

Page 20: Value Types Versus Reference Types
Page 21: Value Types Versus Reference Types

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

• 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

DELEGATES

Page 24: Value Types Versus Reference Types

• 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

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

EVENTS

Page 27: Value Types Versus Reference Types

• 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
Page 29: Value Types Versus Reference Types

DECLARING AN EVENT

• One event

Page 30: Value Types Versus Reference Types

• More than one event

Page 31: Value Types Versus Reference Types

• Static events

Page 32: Value Types Versus Reference Types

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

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

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

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

• Method form and static method

Page 37: Value Types Versus Reference Types

• Anonymous and lambda methods

Page 38: Value Types Versus Reference Types
Page 39: Value Types Versus Reference Types

INTERFACES

Page 40: Value Types Versus Reference Types

• 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

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

GENERICS

Page 43: Value Types Versus Reference Types

• A delegate type may contain generic type parameters.