2.Basic C# Syntax and Semantics
This section introduces you to the basic syntax and semantics of C# programming language.

Basic data types

 

C# Type .NET Framework type Size Range
bool System.Boolean 2 bytes true / false
byte System.Byte Unsigned 8-bit integer 0 to 255
sbyte System.SByte Signed 8-bit integer -128 to 127
char System.Char Unicode 16-bit character U+0000 to U+ffff
decimal System.Decimal 28-29 significant digits ±1.0 Ã— 10−28 to ±7.9 Ã— 1028
double System.Double 15-16 digits (Precision) ±5.0 Ã— 10−324 to ±1.7 Ã— 10308
float System.Single 7 digits (Precision) ±1.5 Ã— 10−45 to ±3.4 Ã— 1038
int System.Int32
-2,147,483,648 to 2,147,483,647
uint System.UInt32 Unsigned 32-bit integer 0 to 4,294,967,295
long System.Int64 Signed 64-bit integer –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ulong System.UInt64 Unsigned 64-bit integer 0 to 18,446,744,073,709,551,615
object System.Object assign values of any type to variables of type object.
short System.Int16 Signed 16-bit integer -32,768 to 32,767

System.UInt16 Unsigned 16-bit integer 0 to 65,535
string System.String represents a string of Unicode characters.

 

Example :

using System;

namespaceSyntaxCS

{

    class Class1

    {

        [STAThread]

        static void Main(string[] args)

        {

            int iCount;               //declaring an integer variable

            char cLetter;           //declaring a character variable

            string strName;       //declaring a string variable

            iCount = 10;

            cLetter = 'L';

            strName = "Easy Steps";

            System.Console.WriteLine(iCount);       //printing into the console

            System.Console.WriteLine(cLetter);

            System.Console.WriteLine(strName);

        }

    }

}

User defined data types

Class

    A class is a way to bind the data and its associated functions together. It allows the data and functions to be hidden if necessary, from external use. When defining a class we are creating a new abstract data type that can be treated like any other built in type.

Syntax : 

[attributes] [modifiers] class [: base-list] { class-body }[;]   

        attributes (Optional)

Additional declarative information.

        modifiers (Optional)

The allowed modifiers are new, abstract, sealed, and the four access modifiers.

        identifier

The class name.

        base-list (Optional)

A list that contains the one base class and any implemented interfaces, all separated by commas.

        class-body

Declarations of the class members.

Example :

Using System;

namespace SyntaxCS

{

    class Person //declaring the class

    {

        private int iAge;

        private string strName;

        public void SetDetails(int age,string name)

        {

            iAge = age;

            strName = name;

        }

        public void GetDetails()

        {

            System.Console.WriteLine(iAge);

            System.Console.WriteLine(strName);

        }

    }

    class Class1

    {

        [STAThread]

        static void Main(string[] args)

        {

            Person FirstPerson = new Person();//creating a class variable (object)

            FirstPerson.SetDetails(27, "Erick");//calling a member function

            FirstPerson.GetDetails();

        }

    }

}

Structure

A structure associates one or more members with each other and with the structure itself. When you declare a structure, it becomes a composite data type, and you can declare variables of that type.

Syntax :

[ attributes] [ modifiers] struct identifier [: interfaces] body [;]

 attributes (Optional)

    Additional declarative information.

 modifiers (Optional)

    The allowed modifiers are new, and the four access modifiers.


identifier

    The struct name.

interfaces (Optional)

A list that contains the interfaces implemented by the struct, all separated by commas.


body
The struct body that contains member declarations.

Example :

using System;

namespace SyntaxCS

{

    struct Person //declaring the structure

    {

        private int iAge;

        private string strName;

        public void SetDetails(int age,string name)

        {

            iAge = age;

            strName = name;

        }

        public void GetDetails()

        {

            System.Console.WriteLine(iAge);

            System.Console.WriteLine(strName);

        }

    }

    class Class1

    {

        [STAThread]

        static void Main(string[] args)

        {

            Person FirstPerson = new Person();//creating a structure variable (object)

            FirstPerson.SetDetails(27, "Erick");//calling a member function

            FirstPerson.GetDetails();

        }

    }

}

 

Program execution control Statements

If...Then...Else Statements

An If...Then...Else statement is the basic conditional statement. If the expression in the If statement is True, the statements enclosed by the If block are executed. If the expression is False, each of the ElseIf expressions is evaluated.

Syntax :

if (expression)
   		statement1
	[else
   		statement2]

Example :

using System;

namespace SyntaxCS

{

    class Class1

    {

        [STAThread]

        static void Main(string[] args)

        {

            int iNumber;

            System.Console.WriteLine("Enter a number");

            iNumber=Convert.ToInt32(System.Console.ReadLine());

            if((iNumber%2)==0)

            {

                System.Console.WriteLine("Number is even");

            }

            else

            {

                System.Console.WriteLine("Number is odd");

            }

        }

    }

}

Select Case

A Select Case statement executes statements based on the value of an expression.

Syntax :

switch (expression)
{
   case constant-expression:
      statement
      jump-statement
   [default:
      
      jump-statement]
}

Example :

using System;

namespaceSyntaxCS

{

    class Class1

    {

        [STAThread]

        static void Main(string[] args)

        {

            char cLetter;

            System.Console.WriteLine("Please enter a letter");

            cLetter=Convert.ToChar(System.Console.ReadLine());

            switch (cLetter)

            {

                case 'a':

                case 'e':

                case 'i':

                case 'o':

                case 'u':

                           System.Console.WriteLine("The letter is a Vowel");

                           break;

                default:

                           System.Console.WriteLine("The letter is not a Vowel");

                           break;

            }

        }

    }

}

While Do While

A While or Do loop statement loops based on a Boolean expression. A While loop statement loops as long as the Boolean expression evaluates to True;

Syntax :

      1) while ( )

      2) do statement while ( expression);

Example :

namespace SyntaxCS

{

   class Class1

   {

       [STAThread]

       static void Main(string[] args)

       {

           int x;

           x = 3;

           while (x < 5)

           {

               Console.WriteLine("while x < 5");

               x += 1;

           }

           do

           {

              Console.WriteLine("Do While x > 0");

            x -= 1;

           }while (x > 0);

      }

}

For Loop

For loop enables you to evaluate a sequence of statements multiple times.

Syntax :

for ([initializers ]; [ expression]; [ iterators]) statement

Example :

using System;

namespace SyntaxCS

{

    class Class1

    {

        [STAThread]

        static void Main(string[] args)

        {

            int iNumber;

            for(iNumber=0;iNumber<10;iNumber++)

            {

                System.Console.WriteLine("VC# in Easy Steps "+(iNumber+1));

            }

        }

    }

}

For Each Loop

A For Each...Next statement loops based on the elements in an expression. A For Each statement specifies a loop control variable and an enumerator expression.

Syntax :

foreach ( type identifier in expression) statement

Example :

using System;

namespace SyntaxCS

{

    class Class1

    {

        [STAThread]

        static void Main(string[] args)

        {

            int iOddCount = 0, iEvenCount = 0;

            int[] iNumberarray = new int [] {0,1,2,5,7,8,11};

            foreach (int itreator in iNumberarray)

            {

                if (itreator%2 == 0)

                    iEvenCount++;

                else

                    iOddCount++;

            }

            Console.WriteLine("Found {0} Odd Numbers, and {1} Even Numbers.",iOddCount, iEvenCount) ;

        }

    }

}



   

Site optimized for IE6 and above. Copyright © 2010. Site Developed Using KTS WebCloud