Wednesday, March 12, 2008

C# Language : The Console Way

Let us see working with C# using notepad and not Visual Studio. Along the way we will explore C# in detail.

It is better for beginner level user to go through this blog before reading my blog on Intermediate Language.
Open notepad and type the following code.

Hello World

class Hello{
 public static void Main()
 {
   System.Console.WriteLine("Shalvin");
 }
}

Save it as 1Hello.cs in say D:

Go to Microsoft Visual Studio 2008 from Program Menu, select Visual Studio Command prompt.
Change directory to D:. Type csc (C# Compiler followed by file name).
d:\>csc 1Hello.cs
The previous statement will create an exe called 1Hello.exe
You can invoke that by typing 1Hello in the command prompt.
d:\>1Hello
In the previous example we created an C# class. Inside the class we are creating a static Main method. We calling the WriteLine method of Console class. The Console class is within System Namespace.
Namespace is a collection of related classes.

Memory Variable and ReadLine
class ReadEg{
public static void Main() {
string strName;
System.Console.Write("Enter you name");
strName = System.Console.ReadLine();
System.Console.WriteLine("Hello " + strName);
}
}

In this example we are declaring a memory variable and ReadLine method for inputing value. Later on we are concatenating the Hello string along with the memory variable and writing it back to the console.

using Statement
using System;
class ReadEg{
public static void Main() {
string strName;
Console.Write("Enter you name");
strName = Console.ReadLine();
Console.WriteLine("Hello " + strName);
}
}

This example is similar to the previous example except that instead of repeating the System in front of Console.ReadLine and Console.WriteLine we are giving an using System; statement.
using statement is a short handing mechanism. That is if you specify an using statement then you need not repeat the namespace name again.

If
using System;
class IfEg
{
  public static void Main()
  {
    string strLang = "C";
    if(strLang == "C#")
    Console.WriteLine("C family of language");
  }
}        

Else
using System;
class IfEg
{
  public static void Main()
  {
    string strLang = "Boo";
    
    if(strLang == "C#")
    Console.WriteLine("C family of language");
    else
    Console.WriteLine("I don't know");
  }
}        

Functions
class FuncEg
{
public static void Main()
{
Hello();
}

public static void Hello()
{
System.Console.WriteLine("Hello");
}
}

Here I am creating a function which doesn't return any value. I am also making the function static so that it can be called from Main which is also static.

using System;

class FuncEg
{
public static void Main()
{
Hello();
}

public static void Hello()
{
for(int i = 0;i<10;i++)
Console.WriteLine("Hello " + i.ToString());
}
}

Date Type Conversions

using System;
class ReadEg{ public static void Main() {
int i, j, res;
Console.WriteLine("Enter first integer :");
i = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter second integer :");
j = Int32.Parse(Console.ReadLine());
res = i + j;
Console.WriteLine(res.ToString());
}
}
In this example I am using Int32.Parse for converting the input to Integer type and ToString() method for converting the result back to string.
Exception Handling

using System;
class ReadEg
{
 public static void Main ()
 {
   try
   {
    int i, j, res;
    Console.WriteLine("Enter first value : ");
    i = Int32.Parse(Console.ReadLine());

    Console.WriteLine("Enter second value : ");
    j = Int32.Parse(Console.ReadLine());

    res = i / j;

    Console.WriteLine(res);
   }
   catch(Exception ex)
   {
    Console.WriteLine("Exception has occured");
   }
  }
}


SqlDataReader
using System;
using Sytem.Data.SqlClient;

class Program
{
static  SqlConnection cnn;
static SqlCommand cmd;
static SqlDataReader dr;
static void Main(string[] args)
{
    cnn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=ContactManagement;Integrated Security=True;Pooling=False");
    cnn.Open();
    cmd = new SqlCommand("select * from ContactGroups", cnn);
    dr = cmd.ExecuteReader();
    while (dr.Read())
        Console.WriteLine("{0}  {1}", dr["GroupId"], dr["GroupName"]);

    Console.ReadLine();
}
}

Windows Forms : The Console Way



.Net Intermediate Language

No comments:

Post a Comment