C# Lab Manual For BCA
C# Lab Manual For BCA
DEPARTMENT OF BCA
PREPARED BY:
P.SREENIVAS, ASST.PROFESSOR
DEPARTMENT OF MCA, PESIT
.Net Laboratory
Hrs / week: 02
Exercises
Time
Sl.
No.
1.
Getting Started with Technology and writing basic programs like Hello
world in C#.
2 Hours
2.
2 Hours
3.
2 Hours
4.
2 Hours
5.
2 Hours
6.
2 Hours
7.
2 Hours
8.
2 Hours
9.
2 Hours
10.
2 Hours
11.
2 Hours
12.
2 Hours
Final Test
2 Hours
class Program
{
static void Main(string[] args)
{
Car car;
car = new Car("Red");
Console.WriteLine(car.Describe());
car = new Car("Green");
Console.WriteLine(car.Describe());
Console.ReadLine();
class Car
{
private string color;
public Car(string color)
{
this.color = color;
using System;
namespace Examples1
{
class Program
{
static void Main(string[] args)
{
int num, i,result;
Console.Write("Enter a number\t");
num = Convert.ToInt32(Console.ReadLine());
for (i = 1; i <= 10; i++)
{
result = num * i;
Console.WriteLine("{0} x {1} = {2}", num, i,result);
}
Console.ReadLine();
}
}
}
//Do-While Loop
using System;
class Program
{
static void Main()
{
int[] ids = new int[] { 6, 7, 8, 10 };
int sum = 0;
int i = 0;
do
{
sum += ids[i];
i++;
} while (i < 4);
System.Console.WriteLine(sum);
Console.ReadLine();
}
}
//while loop
using System;
class Program
{
static void Main()
{
int i = 0;
while (i < 10)
{
Console.Write("While statement ");
Console.WriteLine(i);
i++;
}Console.ReadLine();
}
}
{"b", "f"},
};
Console.WriteLine(array[0, 0]);
Console.WriteLine(array[0, 1]);
Console.WriteLine(array[1, 0]);
Console.WriteLine(array[1, 1]);
}
}
..............................
using System;
public class InitArray
{
Console.WriteLine(array[i]);
}}
//JAGGED ARRAYS..........
Write a Program in C# to find the sum of all the elements present in a jagged array
of 3 inner arrays.
using System;
class MainClass
{
public static void Main (string[] args)
{
int sum = 0;
int[][] x = new int[3][];
x[0]=new int[3];
x[0]=new int[4];
x[0]=new int[5];
for (int j=0; j<=x.Length; j++)
{ for (int k=0; k<=x[j].Length; k++)
{
Console.WriteLine ("enter the"+(k+1)+"elements of the "+
(j+1)+"row");
x [j, k] = int.Parse (Console.ReadLine());
}
}
for (int j=0; j<=x.Length; j++)
{
Console.WriteLine ();
for (int k=0; k<=x[j].Length; k++)
{
Console.Write (x[j,k]+"\t");
}
Console.WriteLine ();
for (int j=0; j<=x.Length; j++)
{
for (int k=0; k<=x[j].Length; k++)
{ sum += x [j, k];
} }
Console.ReadLine();
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace boxing
{
{
double pi=3.142;
Console.WriteLine ("Enter the radius and height");
double r,h,res;
r = double.Parse (Console.ReadLine ());
h = double.Parse (Console.ReadLine ());
res = (2 *pi * r * h) + (2 * pi * r * r);
Console.WriteLine ("Area is"+res);
}
}
class MainClass
{
public static void Main (string[] args)
{
cylinder c=new test()
c.area();
Console.ReadLine();
}
}
{
a = x;
b = y;
c = z;
}
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
public override string ToString()
{
return (String.Format("{0} + {1}i", real, imaginary));
}
public static Complex operator ++(Complex op1)
{
op1.a++;
op1.b++;
op1.c++;
return op1;
}
public void ShowTheResult()
{
Console.WriteLine(a + "," + b + "," + c);
Console.ReadLine();
}
public static void Main()
{
Complex num1 = new Complex(2, 3);
Complex num2 = new Complex(3, 4);
using System.Linq;
using System.Text;
namespace VIRTUAL
{
class shapes
{
public virtual void points()
{
Console.WriteLine("All the geo figures");
}
}
class hexagon : shapes
{
public override void points()
{
Console.WriteLine("hexa has 6 points");
}
}
class circle : shapes
{
public override void points()
{
Console.WriteLine("circle has 0 points");
}
}
class Program
{
static void Main(string[] args)
{
shapes []s={ new shapes(), new hexagon(),new circle()};
for(int i=0;i<s.Length;i++)
{
s[i].points();
}
Console.ReadLine();
}
}
}
8 c) Write a program to demonstrate abstract class and abstract methods in C#.
using System;
abstract class service
{
public abstract void detail();
}
class employee:service
{
String name;
int number, salary;
public override void detail()
{
Console.WriteLine("Enter employee name ");
name=Console.ReadLine();
Console.WriteLine ("Enter employee number");
number=int.Parse(Console.ReadLine());
Console.WriteLine ("Enter employee salary");
salary=int.Parse(Console.ReadLine());
Console.WriteLine ("Name\tnumber\tsalary");
Console.WriteLine (name + "\t" + number + "\t" + salary);
}
}
}
class test
{
public static void Main()
{
}}
9. Write a program
C#(ENCAPSULATION).
to
illustrate
using System;
class Person
{
private string myName ="N/A";
private int myAge = 0;
the
use
of
different
properties
in
{
myName = value;
}
}
public int Age
{
get
{
return myAge;
}
set
{
myAge = value;
}
}
static int count=0;
public static int Count
{
get
{
count++;
return count;
}
}
public override string ToString()
{
return "Name = " + Name + ", Age = " + Age;
}
public static void Main()
{
Console.WriteLine ("USING GET AND SET PROPERTIES");
Person person = new Person();
Console.WriteLine("Person details - {0}", person);
person.Name = "Joe";
person.Age = 99;
get
{
return data[index];
}
set
{
data[index] = value;
}}
}
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc[0] = "Rajesh";
mc[1] = "A3-126";
mc[2] = "Snehadara";
mc[3] = "Bang";
mc[4] = "Mumbai";
Console.WriteLine("{0},{1},{2},{3},{4}",mc[0],mc[1],mc[2],mc[3],mc[4]);
}}
10.
Write a program to implement CONSOLE
formatting, Console IO of both number & strings)
class StandardNumericFormats
{
static void Main()
{
Console.WriteLine("{0:C2}", 123.456);
Console.WriteLine("{0:D6}", -1234);
Console.WriteLine("{0:E2}", 123);
Console.WriteLine("{0:F2}", -123.456);
Console.WriteLine("{0:N2}", 1234567.8);
Console.WriteLine("{0:P}", 0.456);
Console.WriteLine("{0:X}", 254);
}
}
................................................
class CustomNumericFormats
{
static void Main()
I/O
OPERATIONS(Numerical
{
Console.WriteLine("{0:0.00}", 1);
Console.WriteLine("{0:#.##}", 0.234);
Console.WriteLine("{0:#####}", 12345.67);
Console.WriteLine("{0:(0#) ### ## ##}", 29342525);
Console.WriteLine("{0:%##}", 0.234);
}
}
...........................................
//FOR STRINGS.
class UsingReadLine
{
static void Main()
{
Console.Write("Please enter your first name: ");
string firstName = Console.ReadLine();
Console.Write("Please enter your last name: ");
11. Using Try, Catch and Finally blocks write a program in C# to demonstrate error
handling.
using System;
namespace exceptionhandling
{
class MainClass
{
public static void Main (string[] args)
{
intnumbers = new int[2];
try
{
numbers[0] = 23;
numbers[1] = 32;
numbers[2] = 42;
foreach(int i in numbers)
Console.WriteLine(i);
}
catch(IndexOutOfRangeException ex)
{
Console.WriteLine("An index was out of range!\n"+ ex.Message);
}
catch(Exception ex)
{
class MainClass
{
public static void Main (string[] args)
{
test t1 = new test ();
methods m=new methods(t1.strconcat);
m+=t1.strreplace;
m+=t1.strlower;
m();
} }}
Program on Events+Delegates.
using System;
namespace Myevents
{
public class eventtestclass
{
private int nvalue;
public delegate void delvalue();
public event delvalue changed;
protected virtual void callevent(){
if(changed!=null) changed();
else
Console.WriteLine("Event fired but no handler");
}
public eventtestclass(int n)
{ setvalue(n); }
public void setvalue(int nv)
{
if(nvalue!=nv)
{
nvalue=nv;
callevent();
}}
}
class program
{
static void Main()
{
eventtestclass etc=new eventtestclass(3);
etc.setvalue(5);
etc.setvalue(5);
etc.setvalue(3);
Console.ReadLine();
}}
Once u click install it will extract the ISO image in the specified directory.
Once the ISO image is created, I mounded the Disk. This ISO image auto run will launch the
installation. This the first screen of the installation.
Click on the install Microsoft Visual Studio 2010. This will extract the setup in the files and
next u will see the following screen.
Click Next.
Select any of the check box, this will enable Customize button at the bottom left corner.
Select the customize button to select what components you want to install. If you select
both the check boxes all the components will be selected automatically.
If you want you can specify the path where this should be installed, but I am leaving as it is.
And click install
Be patient this will take looooooong timebecause this will install a looooooong list of
items
After installing Microsoft .Net framework .it will ask for a restart.Dont get panic please
go ahead and allow it restart the system. After restarting the system, installation will start
automatically. Once all the components were installed, you will get finish page.
Thats it. Click finish button. You are ready to explore the mush waited and fully loaded
make run
to run it without installing it.
It is a good idea to keep separate copies for using and developing.
If you do install MonoDevelop, it is best to run the current version uninstalled, to make sure
it works, before installing it.
Working on MonoDevelop
Before hacking on MonoDevelop, dont be afraid to ask questions on #monodevelop
IRC or MonoDevelop mailing list. People will be able to give you pointer about where to start
and how best to approach the problems your are trying to solve. There are also number
ofArticles on the MonoDevelop architecture and on implementing addins.
The MonoDevelop solution can be opened from MonoDevelop, and builds can be preformed
form within MonoDevelop. Indeed, some parts of the build (such as Stetic code generation)
must be performed within MD. However, the modified MonoDevelop must be run from a
terminal with
make run
You should follow our contribution rules, for code style and licensing.
After you have made your changes, commit them with a descriptive message and open a
pull request on GitHub.
Troubleshooting the Build
If the MonoDevelop build fails, there are a number of possible fixes.
If the build commands failed, try a clean rebuild:
make clean; make
and fix any that are in conflict, or delete any that have changed unnecessarily.
If the build system failed, check for changed files and re-run the configure script.
Visual Studio
MonoDevelop
Open Ubuntu software center and type mono in the search box,now choose MonoDevelop package in the
list and Install.
In Terminal,
After the installation start MonoDevelop,you should see an aesthetic programming environment.
NOTE:This is very important the console in MonoDevelop 2.4 does not read input.This is a bug in mono
and has already been reported.To execute programs with ReadLine statements please follow the
following steps:
After installing MonoDevelop
Create the program using Vim editor,
vim Demo1.cs