Anda di halaman 1dari 36

C#&.

NET FRAMEWORK
LAB MANUAL

C# AND. NET FRAMEWORK LAB


(COMMON FOR CSE, IT)

LIST OF EXPERIMENTS

1. Classes and objects using out, ref and params


2. Student information system using properties
3. Banking application using inheritance
4. Transaction management using interfaces
5. Solving postfix expressions using stack
6. Solving complex numbers using operator overloading
7. Addition& multiplication using delegates
8. Subscription for exam events using events
9. Calculator using windows application
10.Advanced windows controls

SOFTWARES REQUIRED

VISUAL STUDIO 2010

LANGUAGES

C#

EXERCISE 1.

CLASSES AND OBJECTS USING OUT, REF AND PARAMS


1.a)

using
using
using
using

PASS BY VALUE

System;
System.Collections.Generic;
System.Linq;
System.Text;

namespace exercise1
{
class passbyvalue
{
static void change(int m)
{
m = m + 10;
}
static void Main(string[] args)
{
int x = 100;
change(x);
Console.WriteLine("x=" + x);
Console.Read();
}
}
}

Output

1.b)PASS BY REFERRENCE
3

using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

namespace exercise1b
{
class passbyref
{
static void swap(ref int x, ref int y)
{
int temp = x;
x = y;
y = temp;
}
static void Main(string[] args)
{
int m = 100;
int n = 200;
Console.WriteLine("before swapping");
Console.WriteLine("m=" + m);
Console.WriteLine("n=" + n);
swap(ref m,ref n);
Console.WriteLine("afterswapping");
Console.WriteLine("m=" + m);
Console.WriteLine("n=" + n);
Console.Read();
}
}
}

Output

1.c)OUT PARAMETER
4

using System.Text;
namespace exercise1c
{
class outkey
{
static void square(int x,out int y)
{
y = x * x;
}
static void Main(string[] args)
{
int m;
square(10, out m);
int n = 200;

Console.WriteLine("m=" + m);
Console.Read();

}
}

Output

1.d)PARAMS KEYWORD
5

using System.Text;
namespace exercise1c
{
class paramkey
{
static void parray(params int []arr)
{
Console.Write("array elements are");
foreach (int i in arr)
Console.Write(" " + i);
Console.WriteLine();
}
static void Main(string[] args)
{
int []x={11,22,33};
parray(x);//call 1
parray();//call 2
parray(100, 200);//call 3
Console.Read();
}
}
}

Output

EXERCISE:

2. STUDENT INFORMATION SYSTEM USING PROPERTIES

using System;

using System.Collections.Generic;
using System.Linq;
using System;
namespace tutorialspoint
{
class Student
{
private string code = "N.A";
private string name = "not known";
private int age = 0;
// Declare a Code property of type string:
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
// Declare a Name property of type string:
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}

// Declare a Age property of type int:


public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public override string ToString()
{
return "Code = " + Code + ", Name = " + Name + ", Age = " + Age;
}

class ExampleDemo
{
public static void Main()
{
// Create a new Student object:
Student s = new Student();

// Setting code, name and the age of the student


s.Code = "001";
s.Name = "Zara";
s.Age = 9;
Console.WriteLine("Student Info: {0}", s);

}
}

//let us increase age


s.Age += 1;
Console.WriteLine("Student Info: {0}", s);
Console.ReadKey();

Output

EXERCISE:3.
using
using
using
using

BANKING APPLICATION USING INHERITANCE

System;
System.Collections.Generic;
System.Linq;
System.Text;

namespace BAppl
{
class Account
{
protected string firstName;
protected string lastName;
protected long number;
// protected decimal initialBalance;
public string FirstName
{
set
{
firstName = value;
}
}
public string LastName
{
set
{
lastName = value;
}
}
public long Number
{
set
{
number = value;
}
}
/*
public decimal Initial
{
set
{
initialBalance = value;
}
}*/
public override string ToString()
{
return firstName + " " + lastName + "\nAccount #: " + number ;
}
}
//Checking Account Class:
class CheckingAccount : Account
{

private decimal balance;

public CheckingAccount(string firstName, string lastName, long number, decimal


initialBalance)
{
FirstName = firstName;
LastName = lastName;
Number = number;
Balance = initialBalance;
}
public decimal Balance
{
get

{
}
set
{
}

return balance;

balance = value;

public void deposit(decimal amount)


{
//initial value should be 0 and should be adding 250 to it.
balance += amount;
}
public void withdraw(decimal amount)
{
//this takes the 250 amount and subtracts 98 from it
balance -= amount;
}

public void display()


{
Console.WriteLine(ToString());
Console.WriteLine("Balance: Rs.{0}", balance);
}

//Display class:
class Display
{

static void Main(string[] args)


{
CheckingAccount check = new CheckingAccount("John", "Smith", 123456, 6000);
Console.WriteLine("After Account Creation...");
check.display();

Console.WriteLine("**********************");
Console.WriteLine("After Depositing Rs.6000.");
check.deposit(6000);
check.display();
//constructor
Console.WriteLine("**********************");
Console.WriteLine("After Withdrawing Rs.2000...");
check.withdraw(2000);
check.display();
//constructor
Console.Read();

}
}

Output
10

EXERCISE:4.

TRANSACTION MANAGEMENT USING INTERFACES

using System;
using System.Collections.Generic;

11

using System.Linq;
using System.Text;
namespace InterfaceApplication
{
public interface ITransactions
{
// interface members
void showTransaction();
double getAmount();
}
public class Transaction : ITransactions
{
private string tCode;
private string date;
private double amount;
public Transaction()
{
tCode = " ";
date = " ";
amount = 0.0;
}
public Transaction(string c, string d, double a)
{
tCode = c;
date = d;
amount = a;
}
public double getAmount()
{
return amount;
}
public void showTransaction()
{
Console.WriteLine("Transaction: {0}", tCode);
Console.WriteLine("Date: {0}", date);
Console.WriteLine("Amount: {0}", getAmount());
}
}
class Tester
{
static void Main(string[] args)
{
Transaction t1 = new Transaction("001", "8/10/2012", 78900.00);
Transaction t2 = new Transaction("002", "9/10/2012", 451900.00);
t1.showTransaction();
t2.showTransaction();
Console.ReadKey();
}
}
}

output
12

EXERCISE:5.

SOLVING POSTFIX EXPRESSIONS USING STACK

using System;
using System.Collections.Generic;

13

using System.Linq;
using System.Text;
using System.Collections;
namespace stackexpression
{
class eva
{
public string po;
public string answer;
Stack i = new Stack();
public void e()
{
int a, b, ans;
for (int j = 0; j < po.Length; j++)
{
String c = po.Substring(j, 1);
if (c.Equals ("*"))
{
String sa = (String)i.Pop();
String sb = (String)i.Pop();
a = Convert.ToInt32(sb);
b = Convert.ToInt32(sa);
ans = a * b;
i.Push(ans.ToString());
}
else if (c.Equals("/"))
{
String sa = (String)i.Pop();
String sb = (String)i.Pop();
a = Convert.ToInt32(sb);
b = Convert.ToInt32(sa);
ans = a / b;
i.Push(ans.ToString());
}
else if (c.Equals("+"))
{
String sa = (String)i.Pop();
String sb = (String)i.Pop();
a = Convert.ToInt32(sb);
b = Convert.ToInt32(sa);
ans = a + b;
i.Push(ans.ToString());
}
else if (c.Equals("-"))
{
String sa = (String)i.Pop();
String sb = (String)i.Pop();
a = Convert.ToInt32(sb);
b = Convert.ToInt32(sa);
ans = a - b;
i.Push(ans.ToString());
}
else
{

14

i.Push(po.Substring(j, 1));

}
answer=(String)i.Pop();
}
}
class Program
{
static void Main(string[] args)
{
eva e1 = new eva();
Console.WriteLine("enter any po:stfix expression");
e1.po = Console.ReadLine();
e1.e();
Console.WriteLine("\n\t\tpostfix evaluation: " + e1.answer);
Console.ReadKey();
}
}
}

output

EXERCISE:6.

SOLVING COMPLEX NUMBERS USING OPERATOR


OVERLOADING

using System;
using System.Collections.Generic;

15

using System.Linq;
using System.Text;
namespace operatoroverloading
{
public struct Complex
{
public int real;
public int imaginary;
// Constructor.
public Complex(int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
// Specify which operator to overload (+),
// the types that can be added (two Complex objects),
// and the return type (Complex).
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}

// Override the ToString() method to display a complex number


// in the traditional format:
public override string ToString()
{
return (System.String.Format("{0} + {1}i", real, imaginary));
}

class TestComplex
{
static void Main()
{
Complex num1 = new Complex(2, 3);
Complex num2 = new Complex(3, 4);
// Add two Complex objects by using the overloaded + operator.
Complex sum = num1 + num2;
// Print the numbers and the sum by using the overridden
// ToString method.
System.Console.WriteLine("First complex number: {0}", num1);
System.Console.WriteLine("Second complex number: {0}", num2);
System.Console.WriteLine("The sum of the two numbers: {0}", sum);
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}

Output
16

EXERCISE:7. ADDITION&

using
using
using
using

MULTIPLICATION USING DELEGATES

System;
System.Collections.Generic;
System.Linq;
System.Text;

namespace ConsoleApplication4
{
using System;
delegate int NumberChanger(int n);
namespace example
{

17

class Delegate
{
static int num = 10;
public static int AddNum(int a)
{
num += a;
return num;
}
public static int MultNum(int b)
{
num *= b;
return num;
}
public static int getNum()
{
return num;
}
static void Main(string[] args)
{

}
}

NumberChanger n1 = new NumberChanger(AddNum);


NumberChanger n2 = new NumberChanger(MultNum);
n1(25);
Console.WriteLine("Value of Num: {0}", getNum());
n2(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();

Output

18

EXERCISE:8.

SUBSCRIPTION FOR EXAM EVENTS USING EVENTS


19

using System;
using
using
using
using

System.Collections.Generic;
System.Linq;
System.Text;
System.Threading;

namespace Events
{
class Program
{
static void Main(string[] args)
{
IQTest objTest = new IQTest(); //Publisher
NotificationService objNotify = new NotificationService();
//Subscriber
// += Register a Handler for that Event
objTest.ExamStarted += objNotify.OnExamStarted;

objTest.myExam();
Console.Read();

}
//Publisher or Sender
class IQTest
{
//Delegate
public delegate void ExamEventHandler(object source, EventArgs args);
//Event
public event ExamEventHandler ExamStarted;
public void myExam()
{
Console.WriteLine("Exam is Starting..");
Thread.Sleep(2000);
OnExamStarted();
}

//This method will notify the subscriber


protected virtual void OnExamStarted()
{
// Invoke the event
if (ExamStarted != null)
{
ExamStarted(this, EventArgs.Empty);
}
}

//Subscriber or Receiver
class NotificationService
{
//Handler : That Match Delegate Signature
public void OnExamStarted(object source, EventArgs args)
{
Console.WriteLine("Your time starts now | start answering..");

20

Output

EXERCISE:9.

CALCULATOR USING WINDOWS APPLICATION

Here is a procedure to create a basic calculator. In this section I will build a basic calculator that can
perform the following operations:

21

Addition

Subtraction

Multiplication

Division

Clear

Steps:
Open Microsoft Visual Studio 2010 then click on "File" -> "New" -> "Website".

Then select "ASP.NET Empty Website".

22

Then "Add New Item":

Then select "Web Form":

23

And click "Add". Now it will open the page as in the following:

24

Here we will write our code:.


Process to create calculator
First we need to create a TextBox and 16 Buttons as in the following:

10 Buttons for numbers (0-9)

5 Buttons to perform operation (addition (+), Subtraction (-) , Multiplication (*), Division (/)
,Clear (CLR) )

1 Buttons for evaluation (=)

Default.aspx

Default.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
static float a, c,d;
static char b;
protected void Page_Load(object sender, EventArgs e)
{

25

}
protected void b1_Click1(object sender, EventArgs e)
{
if ((t.Text == "+") || (t.Text == "-") || (t.Text
{
t.Text = "";
t.Text = t.Text + b1.Text;
}
else
t.Text = t.Text + b1.Text;
}
protected void b3_Click(object sender, EventArgs e)
{
if ((t.Text == "+") || (t.Text == "-") || (t.Text
{
t.Text = "";
t.Text = t.Text + b3.Text;
}
else
t.Text = t.Text + b3.Text;
}
protected void b2_Click(object sender, EventArgs e)
{
if ((t.Text == "+") || (t.Text == "-") || (t.Text
{
t.Text = "";
t.Text = t.Text + b2.Text;
}
else
t.Text = t.Text + b2.Text;
}
protected void b4_Click(object sender, EventArgs e)
{
if ((t.Text == "+") || (t.Text == "-") || (t.Text
{
t.Text = "";
t.Text = t.Text + b4.Text;
}
else
t.Text = t.Text + b4.Text;
}
protected void b5_Click(object sender, EventArgs e)
{
if ((t.Text == "+") || (t.Text == "-") || (t.Text
{
t.Text = "";
t.Text = t.Text + b5.Text;
}
else
t.Text = t.Text + b5.Text;
}
protected void b6_Click(object sender, EventArgs e)
{
if ((t.Text == "+") || (t.Text == "-") || (t.Text
{
t.Text = "";
t.Text = t.Text + b6.Text;
}
else

26

== "*") || (t.Text == "/"))

== "*") || (t.Text == "/"))

== "*") || (t.Text == "/"))

== "*") || (t.Text == "/"))

== "*") || (t.Text == "/"))

== "*") || (t.Text == "/"))

t.Text = t.Text + b6.Text;


}
protected void b7_Click(object sender, EventArgs e)
{
if ((t.Text == "+") || (t.Text == "-") || (t.Text
{
t.Text = "";
t.Text = t.Text + b7.Text;
}
else
t.Text = t.Text + b7.Text;
}
protected void b8_Click(object sender, EventArgs e)
{
if ((t.Text == "+") || (t.Text == "-") || (t.Text
{
t.Text = "";
t.Text = t.Text + b8.Text;
}
else
t.Text = t.Text + b8.Text;
}
protected void b9_Click(object sender, EventArgs e)
{
if ((t.Text == "+") || (t.Text == "-") || (t.Text
{
t.Text = "";
t.Text = t.Text + b9.Text;
}
else
t.Text = t.Text + b9.Text;
}
protected void b10_Click(object sender, EventArgs e)
{
if ((t.Text == "+") || (t.Text == "-") || (t.Text
{
t.Text = "";
t.Text = t.Text + b0.Text;
}
else
t.Text = t.Text + b0.Text;
}
protected void sub_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(t.Text);
t.Text = "";
b = '-';
t.Text += b;
}
protected void mul_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(t.Text);
t.Text = "";
b = '*';
t.Text += b;
}
protected void div_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(t.Text);
t.Text = "";

27

== "*") || (t.Text == "/"))

== "*") || (t.Text == "/"))

== "*") || (t.Text == "/"))

== "*") || (t.Text == "/"))

b = '/';
t.Text += b;
}
protected void clr_Click(object sender, EventArgs e)
{
t.Text = "";
}
protected void add_Click1(object sender, EventArgs e)
{
a = Convert.ToInt32(t.Text);
t.Text = "";
b = '+';
t.Text += b;
}
protected void t_TextChanged(object sender, EventArgs e)
{

}
protected void eql_Click(object sender, EventArgs e)
{
c = Convert.ToInt32(t.Text);
t.Text = "";
if (b == '/')
{
d = a / c;
t.Text += d;
a = d;
}
else if (b == '+')
{
d = a + c;
t.Text += d;
a = d;
}
else if (b == '-')
{
d = a - c;
t.Text += d;
a = d;
}
else
{
d = a * c;
t.Text += d;
a = d;
}
}

-----------

output
28

29

30

EXERCISE:10.

ADVANCED WINDOWS CONTROLS


10.a)PROGRESS BAR EXAMPLE

1.GO to Windows application


2.create one progress bar
3.create one button
4.dbl click button control
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace progressbar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int i;
progressBar1.Minimum = 0;
progressBar1.Maximum = 200;

for (i = 0; i <= 200; i++)


{
progressBar1.Value = i;
}

private void Form1_Load(object sender, EventArgs e)


{
}

Output
31

10.b)DATETIMEPICKER CONTROL
1.go to windows application
2.Add DateTimePicker control(All windows form)
32

3.Add one button


4.dbl click button
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace date_time_picker
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
DateTime iDate;
iDate = dateTimePicker1.Value;
MessageBox.Show("Selected date is " + iDate);
}
/*dbl click form window Form1_Load. Appear*/
private void Form1_Load(object sender, EventArgs e)
{
dateTimePicker1.Format = DateTimePickerFormat.Short;
dateTimePicker1.Value = DateTime.Today;
}
}
}

output

33

10.c)SCROLL BAR
1.go to windows application
2.add text box
34

3.dbl click form window


using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

namespace scroll_bar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Multiline = true;
textBox1.ScrollBars = ScrollBars.Both;
}
}

Output

35

36

Anda mungkin juga menyukai