C# notes part ->1



C#.NET
C#.net has been invented by Microsoft. It  is a Component Object Oriented Programming Language of C and C++ family.

It supports
Ø Cross language Integration with .net supported languages.
Ø Multiple class evaluation
Ø Multiple threading
Ø Exception handling
Ø Boxing and Unboxing Techniques
Ø Delegates
Ø Unsigned Data types
Ø Assemblies
C# is a case sensitive language.

Structure of the C#.net program:-

Public class classname
{
    public static void Main()
     {
           // Input Output statements
   }
}

Eg:- Open notepad  and type below program

public  class Sample
{
    public static  void Main()
     {
                   System.Console.WriteLine(“Welcome to C# Programming”);
     }
}
Save this notepad  in D:\DFI\Consoleapp\welcome.cs
Set the c-sharp compiler(csc) path:
Copy the path where csc.exe located:  C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727.
Goto  MyComputer
                             |
Properties
       |
advance Tab
       |
Envijronment Variables
       |
System Variables
       |
Select path (edit)
|
;Paste
       |
    Ok
       |
    Ok
Open command prompt and type   csc
Type below commands at command prompt
Compiling :-- D:\DFI\Consoleapp\>csc Welcome.cs
Executing :--D:\DFI\Consoleapp\>Welcome
Output:-- Welcome to C# Programming

Namespace is a collection of classes.
Collection of namespaces is an Assembly.
The root namespace should be match with the Assembly name.


Namespace n1
{
     public class Sample
     {
            public static void Main()
            {
               ----
                   ----
             }
     }
}




Eg:--
Namespace n1
{
  public  class Sample
  {
    public static  void Main()
     {
                   System.Console.WriteLine(“Welcome to C# Programming”);
     }
  }
}

creating console application through Visual Studio:

StartàprogramsàMicrosoft Visual Studio 2005àVisual Studio2005àFileàNew àProject

Project Type                                    Template

Visual C#                                          Console Application
          Name     : Demo
          Location: D:\DOTNET\Mid\ConsoleApp

                                       OK
namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Welcome");
            System.Console.ReadLine();
        }
    }
}

press F5 key to compile and Debug.

“using” keyword is used to avoid the repetitions of namespace.The main purpose of namespace is to avoid the naming collision of classname.
Eg:  
     System.Console.WriteLine();
        |               |              |
Namespace    Class       Method

using System;
namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("The DotNet Languages are\n");
            Console.WriteLine("C#");
            Console.WriteLine("VB");
            Console.WriteLine("VC++");
           
            Console.ReadLine();
        }
    }
}

Press F5 to view output.

input output statements:

Console.WriteLine() is used to display some message      on the screen.
  Eg:   i).  Console.WriteLine(“Welcome”);
  ii).   Console.WriteLine(string format, Object args);
Eg: int a;
      a=10;
      Console.WriteLine(“{0}”,a);
Console.ReadLine()  is used to read the data from the keyboard as a string.
String str=Consoe.ReadLine();

Write a program to read the data and display it.

using System;
namespace ReadWrite
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            Console.WriteLine("Enter the value of a");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("The value of a is   " + a);
            Console.ReadLine();
        }
    }
}

Accept two numbers and display them.

using System;
namespace ReadWriteAB
{
    class Program
    {
        static void Main(string[] args)
        {
            int a,b;
            Console.WriteLine("Enter the values of a and b");
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("The value of A = {0}  and B = {1}   " , a,b);
            Console.ReadLine();
        }
    }
}

NOTE:  here the Convert is a class which is required to convert one datatype to another datatype.

Boxing is a concept of storing the value type as a reference type.
Unboxing is a concept of storing the reference type as a value type.

Operators:
Arithmetic       +, -, *, %, /.
Comparison     <, >, <=, >=, !, ==
Logical             &&, ||, !=
Concatenation  +


using System;
namespace OperatoreEx
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b;
            Console.WriteLine("Enter the values of a and b");
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(" A+B={0}, A-B={1}, A*B={2}, A%B={3}, A/B={4} ",a+b,a-b,a*b,a%b,a/b);
            Console.ReadLine();

        }
    }
}

Conditional Statements
if else
switch-case

loops
while
do-while
for
foreach

foreach(datatype variable in list)
{                                          |     
       …………                          array list
}
Eg:
    
using System;
namespace ForeachEg
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] Score = { 100, 20, 30 };
            foreach (int i in Score)
            {
                Console.WriteLine(i+"\t");
            }
            Console.ReadLine();
        }
    }
}


ARRAYS

Array is a collection of homogeneous elements which will be stored in a consecutive memory location.
Eg: A={10,20,30,40};
A[0]=10
A[1]=20

A
10
20
30
40
  0    1     2    3    àindex



Syntax:  Datatype [] Arrayname;
Eg: int [] Score;
      string [] StuName;

Array Types

1.  Single Dimension array
2.  Multi Dimension array
3.  Jagged array

Note: array is Base class for all the properties and methods.

Array Properties: Length, Rank
Array Methods: GetLength(), Sort(), Copy().

1. Single Dimension array:

Syntax:  

  a).datatype [] arrayname;
                Arrayname=new datatype[size];
        Eg:   int [] Score;
                 Score =new int[5];
  b).datatype [] arrayname =new datatype[size];
        Eg:   int [] Score=new int[5];
  c).datatype[] arrayname={collection of elements};
      Eg: int [] Score={100,20,30,40};




using System;

namespace SingleDimArrayEx
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] Score = new int[5];
            Console.WriteLine("enter the elements");
            for (int i = 0; i < Score.Length; i++)
            {
                Score[i] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("the values of the array are");
            for (int i = 0; i < Score.Length; i++)
            {
                Console.Write(Score[i] + "\t");
            }
            Console.WriteLine();
            Array.Sort(Score);
            Console.WriteLine("your sorted array is ");
            for (int i = 0; i < Score.Length; i++)
            {
                Console.Write(Score[i] + "\t");
            }
            Console.ReadLine();

        }
    }
}





Multi Dimension Array:
Syntax:
            Datatype [,] arrayname;
Datatype [,] arrayname=new datatype[row size,column size];

eg: int[,] Test =new int[2,3];

using System;
namespace multiarrayapp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] MulScore= new int[2, 2];
            Console.WriteLine("enter the elements into an array");
            for (int i = 0; i < MulScore.GetLength(0); i++)
            {
                for (int j = 0; j < MulScore.GetLength(1); j++)
                {
                    MulScore[i, j] = Convert.ToInt32(Console.ReadLine());
                                
                }
           
            }
            Console.WriteLine();
            Console.WriteLine("the array values are");
            for (int i = 0; i < MulScore.GetLength(0); i++)
            {
                for (int j = 0; j < MulScore.GetLength(1); j++)
                {
                    Console.Write(MulScore[i, j] + "\t");
                }
                Console.WriteLine();
            }

        
            Console.ReadLine();
        }
    }
}



Jagged Array:

Arrays of an array is called as a jagged array where rows are fixed and columns are not fixed.

Eg:

10
20

30
40
50
60

70
80 
90
100


Syntax:
     Datatype [][] jagarrayname = new datatype [rowsize][];

int [][] JagScore = new int[5][];

JagScore[0]=new int[2] {10,20};
JagScore[1]=new int[3] {30,40,50};
JagScore[2]=new int[1] {60};
JagScore[3]=new int[3] {70,80,90};
JagScore[4]=new int[1] {100};                                                                               
using System;
namespace jarray
{
    class Program
    {
        static void Main(string[] args)
        {
            int[][] JagArray = new int[5][];
            JagArray[0] = new int[2] { 10, 20 };
            JagArray[1] = new int[3] { 30, 40, 50 };
            JagArray[2] = new int[1] { 60 };
            JagArray[3] = new int[3] { 70, 80, 90 };
            JagArray[4] = new int[1] { 100 };
            Console.WriteLine("\n\n\n your jagged array is \n");
            for (int i = 0; i < JagArray.GetLength(0); i++)
            {
                for (int j = 0; j < JagArray[i].Length; j++)
                {
                    Console.Write(JagArray[i][j] + "\t");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}


Comments

Popular posts from this blog

WCF interview questions

The term 'Connect-MsolService' is not recognized as the name of a cmdlet, function, script file, or operable program

what is Event Cache table in sharepoint