What is C#?


C# (pronounced "see sharp" or "C Sharp") is one of many .NET programming languages. It is object-oriented and allows you to build reusable components for a wide variety of application types. Microsoft introduced C# on June 26th, 2000 and it became a v1.0 product on Feb 13th 2002.

C# is an evolution of the C and C++ family of languages. However, it borrows features from other programming languages, such as Delphi and Java. If you look at the most basic syntax of both C# and Java, the code looks very similar, but then again, the code looks a lot like C++ too, which is intentional. Developers often ask questions about why C# supports certain features or works in a certain way. The answer is often rooted in it's C++ heritage.


How Do I Get Started?

The C# Tutorial was created to help beginning developers and other professionals who need a quick on-ramp to the language. Plz Click Start Menu then
Visual Studio 2008. Open A Dialog Box Select New Project.
14:30C# Tutorial

How Do I Get Started?


The C# Tutorial was created to help beginning developers and other professionals who need a quick on-ramp to the language. Plz Click Start Menu then
Visual Studio 2008. Open A Dialog Box Select New Project.
14:30C# Tutorial

The C# Keywords

abstract as base bool break

Byte case catch char checked

class const continue decimal default

delegate do double else enum

event explicit extern false finally

fixed float for foreach goto

if implicit in int interface

internal is lock long namespace

new null object operator out

override params private protected public

readonly ref return sbyte sealed

short sizeof stackalloc static string

struct switch this throw true

try typeof uint ulong unchecked

unsafe ushort using virtual volatile
14:24C# Tutorial

Csharp Data Types

An important part in the life of a C# programmer is the management of data types. Just as in C and C++, every variable has a fixed data type. Every data type provides a rich set of operators that can be used to perform a very special operation. Objects can be seen as data types as well, but we'll take a closer look at that later in this book.


C# Type Mono Signed Memory Range


Sbyte System.Sbyte Yes 1 byte –128 to 127

Short System.Int16 Yes 2 bytes –32768 to 32767

Int System.Int32 Yes 4 bytes –2147483648 to 2147483647

Long System.Int64 Yes 8 bytes –9223372036854775808 to 9223372036854775807

Byte System.Byte No 1 byte 0 to 255

Ushort System.Uint16 No 2 bytes 0 to 65535

Uint System.Uint32 No 4 bytes 0 to 4294967295

Ulong System.Uint64 No 8 bytes 0 to 18446744073709551615

Float System.Single Yes 4 bytes –1.5x10-45 to 3.4 x x1038

Double System.Double Yes 8 bytes –5.0x10-324 to 1.7x10308

Decimal System.Decimal Yes 12 bytes 1.0x10-28 to 7.9x1028

Char System.Char 2 bytes Unicode characters
Boolean System.Boolean 1 byte True or false
After this brief overview, we'll show you how variables and data types can be used efficiently. The following example shows how a variable can be declared and displayed on screen:
using System;

class Hello
{
public static void Main()
{
int x = 3;
Console.WriteLine("The value is " + x );
}
}
Declaring a variable works just like in C and C++. The way data is displayed is reminiscent of Java. We use the plus operator to connect two strings with each other. The output is not surprising:
[hs@localhost csharp]$ mono hello4.exe
The value is 3


Using the plus operator is truly easy, but it could also be a danger. In most cases, the plus operator is used as a mathematical operator. If it's used differently, the result might be a bit unexpected:
using System;

class Hello
{
public static void Main()
{
uint x = 3;
Console.WriteLine("The value is " + x + 1 );
Console.WriteLine("The value is " + (x + 1) );
}
}
We perform two operations that look pretty similar. However, the results differ significantly:
[hs@localhost csharp]$ mono hello5.exe
The value is 31
The value is 4
The first operation connects two strings. The second example performs an addition. As you can see, the plus operator has more than just one meaning.
14:21C# Tutorial

Csharp Explicit Conversion

using System;
class Program
{
static void Main(string[] args)
{
double x = 2.1;
int y = 12;
int z = (int)x + y; //Explicit conversion from double to int
Console.WriteLine(z);
Console.Read();
}
}
14:20C# Tutorial

Csharp Implicit Conversion

using System;

class Program
{
static void Main(string[] args)
{
int x = 2;
double y = 12.2;
double z;
z = x + y; //x is automatically converted into a double
Console.WriteLine(z);
Console.Read();
}
}
Output
14.2
This is an example of csharp implicit conversion. The compiler implicitly converts x to a double. Data loss is not an issue with this operation.
14:20C# Tutorial

What is Conversion?

Conversion is the process of changing a value from one type to another.
• Implicit: Automatic compiler conversion where data loss is not an issue.
• Explicit: A conversin where data loss may happen and is recommended that the programmer writes additional processing

A common rule of thumb is that it is much easier to convert up then it is to convert down. For example, conversion from int to long is an easy operation, but converting the other way around is not so easy. Remember the long data type is a bigger type then the int data type is. To prevent data loss just remember to convert from small to large.

There may be situations where you cannot get around from converting a large data type to small. This where explicit conversion comes into play.
14:19C# Tutorial