Advertisement

Responsive Advertisement

III. Nullable Type, IV. Arrays and Collection

 


III. Nullable Type

As you know, a value type cannot be assigned a null value. For example, int i = null will give you a compile time error.


C# 2.0 introduced nullable types that allow you to assign null to value type variables. You can declare nullable types using Nullable<t> where T is a type.

A nullable type can represent the correct range of values for its underlying value type, plus an additional null value. For example, Nullable<int> can be assigned any value from -2147483648 to 2147483647, or a null value.


IV. Arrays and Collection

Array

Array is a group of similar data type which is fixed in size. Under array there is no process of boxing and unboxing because Array is Strongly typed.

In above example, array type is int and the name of the array is A which is displaying all the values of array A: 1,2,3,4,5,6.

Generally a developer prefer to use Array because there is no boxing and unboxing. Suppose there is one School and the principal of school would like to know only the name of the student of branch CSE, so it is better to use an array because we know how many students are there in CSE branch and also we know that the type of Student Name is string.

String[]Student_Name=new String[60]{‘ Amit’……….’Sandep’}

This will contain only Student Name. There are the following three types of Array: 

  1. Single Dimensional Array
  2. Multi-Dimensional Array
  3. Jagged Array

Collection

Collection is a group of homogenous and heterogeneous data type. Size of Array is not fixed and also Collection is not strong type. We use Generic to make Collection as Strong type. In C# we use namespace System.Collection.Generic to implement Generic on Collection.

Syntax of Collection

Collection<data type>Name =new <data type>();

Generally we use Collection when there is Heterogeneous data type object. Suppose there is one School and the principal of the school would like to know Student Id, Student Name and Course Name of student of Branch CSE, so it is better to use Collection because we have to display the data of Heterogeneous data type object. Like,


Array is a group of homogeneous data type object which is fixed in size and this is strongly typed. Collection is a group of homogeneous and heterogeneous data type object which is not fixed in size and thus is not strongly typed. But we use Generic to make Collection as strong Type.

Difference between Array and Collection:



Post a Comment

0 Comments