array of integer values, like this:

int[] nums;

In this case, nums is an array that has yet to contain any elements (of type int). To make nums an array containing 10 elements, you can instantiate it with the new keyword followed by the type name and then the size of the array:

nums = new int[10];

The index for each element in the array starts from 0 and ends at n-1 (where n is the size of the array). To assign a value to each element of the array, you can specify its index as follows:

nums[0] = 0;

nums[1] = 1;

//...

nums[9] = 9;

Arrays are reference types, but array elements can be of any type.

Instead of assigning values to each element in an array individually, you can combine them into one statement, like this:

int[] nums = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

Arrays can be single-dimensional (which is what you have seen so far), multi-dimensional, or jagged. You'll find more about arrays in Chapter 13, in the discussion of collections.

Implicit Typing

In the previous versions of C#, all variables must be explicitly typed-declared. For example, if you want to declare a string variable, you have to do the following:

string str = 'Hello World';

In C# 3.0, this is not mandatory — you can use the new var keyword to implicitly declare a variable. Here's an example:

var str = 'Hello world!';

Here, str is implicitly declared as a string variable. The type of the variable declared is based on the value that it is initialized with. This method of variable declaration is known as implicit typing. Implicitly typed variables must be initialized when they are declared. The following statement will not compile:

var str; //---missing initializer---

Also notice that IntelliSense will automatically know the type of the variable declared, as evident in Figure 3-10.

Figure 3-10

You can also use implicit typing on arrays. For example, the following statement declares points to be an array containing two Point objects:

var points = new[] { new Point(1, 2), new Point(3, 4) };

When using implicit typing on arrays, all the members in the array must be of the same type. The following won't compile since its members are of different types — string and Boolean:

//---No best type found for implicitly-typed array---

var arr = new[] { 'hello', true, 'world' };

Implicit typing is useful in cases where you do not know the exact type of data you are manipulating and want the compiler to determine it for you. Do not confuse the Object type with implicit typing.

Variables declared as Object types need to be cast during runtime, and IntelliSense does not know their type at development time. On the other hand, implicitly typed variables are statically typed during design time, and IntelliSense is capable of providing detailed information about the type. In terms of performance, an implicitly typed variable is no different from a normal typed variable.

Implicit-typing is very useful when using LINQ queries. Chapter 14 discusses LINQ in more detail.

Type Conversion

C# is a strongly typed language, so when you are assigning values of variables from one type to another, you must take extra care to ensure that the assignment is compatible. Consider the following statements where you have two variables — one of type int and another of type short:

int num;

short sNum = 20;

The following statement assigns the value of sNum to num:

num = sNum; //---OK---

This statement works because you're are assigning the value of a type (short) whose range is smaller than that of the target type (int). In such instances, C# allows the assignment to occur, and that's known as implicit conversion.

Converting a value from a smaller range to a bigger range is known as widening.

The following table shows the implicit conversion between the different built-in types supported by C#.

Convert from (type) To (type)
sbyte short, int, long, float, double, or decimal
byte short, ushort, int, uint, long, ulong, float, double, or decimal
short int, long, float, double, or decimal
ushort int, uint, long, ulong, float, double, or decimal
int long, float, double, or decimal
Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату