FREE BOOK

Chapter 1: Introduction to C#

Posted by Apress Free Book | C# Language December 08, 2008
This Chapter will introduce you to C#. You will learn how to write and compile C# programs, C# syntaxes, data types, control flow, classes and their members, inter-faces, arrays, and exception handling I'll begin with an overview of the language.

Type Conversions

C# supports two kinds of type conversions: implicit conversions and explicit conversions. Some of the predefined types define predefined conversions, such as converting from an int type to a long type.

Implicit conversions are conversions in which one type can directly and safely are converted to another type. Generally, small range type converts to large range type. As an example, you'll examine the process of converting from an int type to a long type. In this conversion, there is no loss of data, as shown in Listing 1-18.

Listing 1-18 Conversion example

using System;
class
ConversionSamp
{
    static void Main()
    {
        int num1 = 123;
        long num2 = num1;
        Console.WriteLine(num1.ToString());
        Console.WriteLine(num2.ToString());

    }
}

Casting performs explicit conversions. There may be a chance of data loss or even some errors in explicit conversions. For example, converting a long value to an integer would result in data loss.

This is an example of an explicit conversion:

long num1 = Int64.MaxValue;
int
num2 =(int)num1;
Console.WriteLine(num1.ToString());
Console.WriteLine(num2.ToString());


The process of converting from a value type to a reference type is called boxing. Boxing is an implicit conversion. Listing 1-19 shows an example of boxing.

Listing 1-19 boxing example

using System;
class
ConversionSamp
{
    static void Main()
    {
        int num1 = 123;
        Object obj = num1;
        Console.WriteLine(num1.ToString());
        Console.WriteLine(obj.ToString());
    }
}

The process of converting from a reference type to a value type is called unboxing. Listing 1-20 shows an example of unboxing.

Listing 1-20 Unboxing example

using System;
class
ConversionSamp
{
    static void Main()
    {
        Object obj = 123;
        int num1 = (int)obj;
        Console.WriteLine(num1.ToString());
        Console.WriteLine(obj.ToString());
    }
}

Attributes

Attributes enable the programmer to give certain declarative information to the elements in their class. These elements include the class itself, the methods, the fields, and the properties. You can choose to use some of the useful built-in attributes provided with the .NET platform, or you can create your own. Attributes are specified in square brackets ( [. . .] ) before the class element upon which they're implemented. Table 1-6 shows some useful attributes provided with .NET.

Table 1-6 Useful Built-in Attributes

NAME DESCRIPTION EXAMPLE
DllImport Imports a native DLL [DllImport("winmm.dll") ]
Serializable Makes a class serializable [Serializable]
Conditional Includes/omits a method based on condition [Conditional(Diagnostic")]

Total Pages : 10 45678

comments