2. How to execute a C# program?
Install setup package named dotNetFx40_Full_setup.exe can be
downloaded from Microsoft’s general download area
http://www.microsoft.com/en-us/download/details.aspx?id=17851
Set Path varable through Mycomputer’s property –Environment
Variable
PATH: C:WINDOWSMicrosoft.NETFrameworkv4.0.30319
Use Command prompt and then type the following command for
compilation :
c:mydircsc programname.cs
To run the C# program
c:mydirprogramname
Prof. Syed Mustafa(Ph.D) 2
3. Program to add 2 numbers
// type the program in notepad & save the program as
“add.cs”
using System;
class add
{
public static void Main()
{
Console.WriteLine("Enter 2 numbers");
int a=int.Parse(Console.ReadLine());
int b=int.Parse(Console.ReadLine());
int c=a+b;
Console.WriteLine("sum of "+a+" and "+b+" is "+c);
}
}
Prof. Syed Mustafa(Ph.D) 3
4. Program to display Command line
arguments
// type the program in notepad & save the program as
“cmd.cs”
using System;
class cmd
{
public static void Main(string[] s )
{
Console.WriteLine("command line arguments");
for(int i=0;i<s.Length;i++)
Console.WriteLine(s[i]);
}
}
Prof. Syed Mustafa(Ph.D) 4
5. Program to display Command line
arguments
// type the program in notepad & save the program as
“cmd.cs”
using System;
class cmd
{
public static void Main(string[] s )
{
Console.WriteLine("command line arguments");
foreach(string i in s)
Console.WriteLine(i);
}
}
Prof. Syed Mustafa(Ph.D) 5
6. Program to display Command line
arguments
// type the program in notepad & save the program as
“cmd.cs”
using System;
class cmd
{
public static void Main(string[] s )
{
string[] a=Environment.GetCommandLineArgs();
Console.WriteLine("command line arguments");
for(int i=1;i<a.Length;i++) //s[0] is cmd.cs, here s[1] is
first argument
Console.WriteLine(a[i]);
}
} Prof. Syed Mustafa(Ph.D) 6
7. Program to use Math class
using System;
class sqt
{
public static void Main()
{
Console.WriteLine("Enter number");
int n=int.Parse(Console.ReadLine());
double r=Math.Sqrt(n);
Console.WriteLine("square root of "+n+" is "+r);
}
}
Prof. Syed Mustafa(Ph.D) 7
8. Program to use Exception
using System;
class Except1
{
public static void Main()
{
try{
int a=5,b=0;
Console.Write(“Multiple Exception");
int c=a/b;
Console.WriteLine("C="+c);
}catch(DivideByZeroException e)
{
Console.WriteLine("Divide by 0 Error");
}catch(ArithmeticException ae)
{
Console.WriteLine("Arithmetic Error");
}
}
}
Prof. Syed Mustafa(Ph.D) 8
9. Program to use Exception
using System;
class Except1
{
public static void Main()
{
try{
int a=5,b=0;
Console.Write(“Multiple Exception");
int c=a/b;
Console.WriteLine("C="+c);
}catch(DivideByZeroException e)
{
Console.WriteLine("Divide by 0 Error");
}catch(ArithmeticException ae)
{
Console.WriteLine("Arithmetic Error");
} finally
{
Console.WriteLine("From finally...");
}
}
}
Prof. Syed Mustafa(Ph.D) 9
10. Program to use Exception
using System;
class Except3
{
public static void Main()
{
try{
int age=14;
if (age<15) throw new ArithmeticException();
Console.WriteLine("Age=" + age);
}catch(ArithmeticException ae)
{
Console.WriteLine("Age Error...");
}finally
{
Console.WriteLine("From finally...");
}
}
}
Prof. Syed Mustafa(Ph.D) 10
11. Program to use Exception
using System;
class Except4
{
public static void Main()
{
int a=893434,b=78888;
try{
int c=checked(a*b);
Console.WriteLine("a=" + a);
}catch(OverflowException ae)
{
Console.WriteLine("Overflow Error...“+ae.Message);
Console.WriteLine(“Source Error...“+ae.Source);
}finally
{
Console.WriteLine("From finally...");
}
}
}
Prof. Syed Mustafa(Ph.D) 11
12. Program to use Exception
using System;
class Except
{
public static void Main()
{
try{
int a=5,b=0;
Console.WriteLine(“Exception");
int c=a/b;
Console.WriteLine("C="+c);
}catch(DivideByZeroException e)
{
Console.WriteLine("Divide by 0 Error: "+e);
Console.WriteLine("Error Message is: "+e.Message);
Console.WriteLine("Stack trace is: "+e.StackTrace);
Console.WriteLine("Error Source is: "+e.Source);
Console.WriteLine("Target Site is: "+e.TargetSite);
Console.WriteLine("HelpLink is: "+e.HelpLink);
}
}
}
Prof. Syed Mustafa(Ph.D) 12
13. Program to use customized Exception
using System;
class AgeException:Exception
{public AgeException(String msg):base(msg){ }
}
class Except4custom
{public static void Main()
{ int age=4;
try{
if(age<15) throw new AgeException("Under Age");
Console.WriteLine("age=" + age);
}catch(AgeException ae)
{
Console.WriteLine("Error is...:"+ae);
}finally
{
Console.WriteLine("From finally...");
}
}
}
Prof. Syed Mustafa(Ph.D) 13
14. Program to use customized Exception
using System;
class AgeException:Exception
{String m;
public AgeException(String msg)
{m=msg;
}
public override string Message
{
get { return m; }
}
}
class Except4custom2
{public static void Main()
{ int age=4;
try{
if(age<15) throw new AgeException("Under Age");
Console.WriteLine("age=" + age);
}catch(AgeException ae)
{
Console.WriteLine("Error is...:"+ae.Message);
}
}
Prof. Syed Mustafa(Ph.D) 14
}
15. Program to use customized Exception
using System;
class AgeException:Exception
{ String m;
public AgeException(String msg)
{m=msg;
}
public override string Message
{
get { m+="Age is not valid"; return m; }
}
}
class Except4custom1
{ public static void Main()
{ int age=4;
try{
if(age<15) throw new AgeException("Under Age");
Console.WriteLine("age=" + age);
}catch(AgeException ae)
{
Console.WriteLine("Error is...:"+ae.Message);
}
}
Prof. Syed Mustafa(Ph.D) 15
}