|
.NET constants, enum, arrays and collection - October 30, 2008
at 18:10 pm by Amit Satpute
Describe how to create and use array in .NET.
Arrays treat several items as a single collection.
Following are the ways to create arrays:
Declaring a reference to an array
Int32[] a;
Create array of ten Int32elements
b = new Int32[10];
Creating a 2-dimensional array
Double[,] c = new Double[10, 20];
Creating a 3-dimensional array
String[,,] d = new String[5, 3, 10];
Define Constants and Enumerations in .NET.
Constants are values which are known at compile time and do not change.
Example:
Module Module1
Sub Main()
Const abc = "100.00"
Console.WriteLine(abc)
End Sub
End Module
An enumeration is a named constant. Enum provides methods to:
-
compare instances of classes,
-
convert the instance values to strings,
-
convert strings to an instance of a class,
-
create instance of a specified enumeration and value.
Explain collection in .NET and describe how to enumerate through the members of
a collection.
There are various collection types in .NET which are used for manipulation of
data. Collections are available in System.Collections namespace
IEnumerator is the base interface for all enumerators. Enumerators can only read
the data from the data collections but not modify them. Reset and MoveNext are
the methods used with enumerators which bring it back to the first position and
move it to the next element position respectively. Current returns the current
object.
Summarize the similarities and differences between arrays and collections.
The Array class is not part of the System.Collections namespaces Collections
are. But an array is a collection, as it is based on the IList interface.
Array has a fixed capacity but the classes in the System.Collections namespaces
don’t.
However, only the system and compilers can derive explicitly from the Array
class. Users should use the array constructs provided by the language that they
use.
Briefly describe the two kinds of multidimensional arrays in .NET.
There are two types of multidimensional arrays:
Rectangular arrays
These multidimensional arrays have all the sub-arrays with a
particular dimension of the same length. You need use a single set of square
brackets for rectangular arrays.
Jagged arrays
These multidimensional arrays have each sub-array as independent
arrays of different lengths. With these you need to use a separate set of
square brackets for each dimension of the array.
|