0% found this document useful (0 votes)
12 views27 pages

Net 1 8

The document contains practical exercises in C# programming, including user input handling, calculator functionality, temperature and currency conversion, and various C# concepts such as constructors, method overloading, inheritance, and error handling. It also includes examples of properties, indexers, and a Windows Forms application for an arithmetic calculator. Each section provides code snippets and expected outputs for better understanding and implementation of the concepts.

Uploaded by

Riddhi Vekariya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views27 pages

Net 1 8

The document contains practical exercises in C# programming, including user input handling, calculator functionality, temperature and currency conversion, and various C# concepts such as constructors, method overloading, inheritance, and error handling. It also includes examples of properties, indexers, and a Windows Forms application for an arithmetic calculator. Each section provides code snippets and expected outputs for better understanding and implementation of the concepts.

Uploaded by

Riddhi Vekariya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

.

NET Technology -202045604

PRACTICAL 01

1) Write C# code to prompt a user to input his/her name and country


name and then the output will be shown as an example below: Hello
Ram from country India!

Code:
using System;
namespace practical1
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter your name");
string name=Console.ReadLine();
Console.WriteLine("enter your country");
string country=Console.ReadLine();
Console.WriteLine("hello "+name+" from country "+country);
}
}
}

Output:

2) Create console application for Calculator.

Code:
using System;
namespace calculator
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter num 1");
int num1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter num2:");
int num2 = int.Parse(Console.ReadLine());
Console.WriteLine("1.Addition:");

1
12202040501079 Pushti Baldha
.NET Technology -202045604

Console.WriteLine("2.Subtarction:");
Console.WriteLine("3.Multiplication:");
Console.WriteLine("4.Division");
Console.WriteLine("Enter your choice");
int ch = int.Parse(Console.ReadLine());
switch (ch)
{
case 1:
Console.WriteLine("addition:" + (num1 + num2));
break;
case 2:
Console.WriteLine("subtarction" + (num1 - num2));
break;
case 3:
Console.WriteLine("Multiplicaation" + (num1 * num2));
break;
case 4:
Console.WriteLine("division" + (num1 / num2));
break;
default:
Console.WriteLine("kal ana");
break;

}
}
}
}
Output:

2
12202040501079 Pushti Baldha
.NET Technology -202045604

Practical-02
Write C# code to do the following
1) Perform Celsius to Fahrenheit Conversion and Fahrenheit to Celsius
conversion.

Code:
using System;
namespace ferenight_celcius
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the number to convert");
float num1 = float.Parse(Console.ReadLine());
Console.WriteLine("1.Fahrenheit to Celsius");
Console.WriteLine("2.Celsius to Fahrenheit");
Console.WriteLine("Enter your choice:");
int ch = int.Parse(Console.ReadLine());
switch (ch)
{
case 1:
Console.WriteLine("the Fahrenheit to Celsius is: " + (((num1 - 32) * 5)/ 9));
break;
case 2:
Console.WriteLine("the celsius to farehneit is: " + ((num1 * 9 / 5) - 32));
break;
default:
Console.WriteLine("choose coorect choice");
break;
}
}
}
}

Output:

3
12202040501079 Pushti Baldha
.NET Technology -202045604

2) Perform currency conversion.Rupees to dollar, frank, euro.

Code:
using System;
namespace currency_convertor
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the rupees to get converted:");
double num1=double.Parse(Console.ReadLine());
Console.WriteLine(+num1+"Rupees to dollar = " + (num1 / 85.55));
Console.WriteLine(+num1 + "Ruppes to frank = " + (num1 / 94.09));
Console.WriteLine(+num1+"Rupees to euro = "+(num1/88.35));

}
}
}

Output:

4
12202040501079 Pushti Baldha
.NET Technology -202045604

PRACTICAL-03
Create console applications to implement following C# Concepts.
1. Static Constructor & Constructor Overloading.
2. Method Overloading
3. Structured Error Handling
4. Inheritance
5. Params parameter
6. Delegates and Events out and ref

1) Static Constructor & Constructor Overloading


Code:
using System;
public class Account
{
public int id;
public string name;
public static float rateofInterest;

public Account()
{
id = 0;
name="Account holder";
}

public Account(int i, string n)


{
id = i;
name = n;
}
static Account()
{
rateofInterest = 9.5f;
}
public void display()
{
Console.WriteLine(id+" ,Your Name is: "+name+ ", Rate of interest is: "+rateofInterest);
}
}
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter id:");
int i=int.Parse(Console.ReadLine());
Console.WriteLine("Enetr name:");
string name=Console.ReadLine();
Account acc = new Account(i,name);
Account acc2 = new Account();
acc.display();
acc2.display();

}
}

5
12202040501079 Pushti Baldha
.NET Technology -202045604

Output:

2) Method Overloading

Code:
using System;
using prac3_2;

namespace prac3_2
{
internal class Calculate
{
public void sqaure(int a)
{
Console.WriteLine("The square is:"+ (a*a));
}
public void sqaure(double a)
{
Console.WriteLine("The square is:" + (a * a));
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the number: ");
int a = int.Parse(Console.ReadLine());
Calculate c1=new Calculate();
c1.sqaure(a);

Console.WriteLine("Enter the number: ");


double n = double.Parse(Console.ReadLine());
c1.sqaure(n);
}
}

Output:

6
12202040501079 Pushti Baldha
.NET Technology -202045604

3) Structured Error Handling

Code:

using System;
namespace prac3_3
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter num1:");
int i = int.Parse(Console.ReadLine());
Console.WriteLine("Enter num2:");
int j = int.Parse(Console.ReadLine());
try
{
int res;
res = (i / j);
Console.WriteLine("Result is:" + res);
}
catch (DivideByZeroException e)
{
Console.WriteLine(e.Message);
}
/*finally
{
Console.WriteLine("Enter correct input:");
}*/
}
}
}

Output:

7
12202040501079 Pushti Baldha
.NET Technology -202045604

4) Inheritance

Code:

using System;
class Animal
{
public void Speak()
{
Console.WriteLine("Animal speaks");
}
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Dog barks");
}
}
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog();
dog.Speak();
dog.Bark();
}
}
Output:

8
12202040501079 Pushti Baldha
.NET Technology -202045604

5) Parms Parameter
Code:

using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Sum(1, 2, 3));
Console.WriteLine(Sum(1, 2, 3, 4, 5));
}
static int Sum(params int[] numbers)
{
int sum = 0;
foreach (int number in numbers)
{
sum += number;
}
return sum;
}
}
Output

6) Delegates and Events

Code:
using System;
using System.Collections.Generic;
class Program
{
static int calculateSum(int x, int y)
{
return x + y;
}
public delegate int myDelegate(int num1, int num2);

static void Main()


{
myDelegate d = new myDelegate(calculateSum);
int result = d(5, 6);
Console.WriteLine(“The sum is :” + result);
}
}

Output:

9
12202040501079 Pushti Baldha
.NET Technology -202045604

7) Out & ref

Code:

using System;
class RefOutExample
{
public void Ref(ref int number)
{
number += 10;
}
public void Out(out int result)
{
result = 20;
}
static void Main()
{
RefOutExample example = new RefOutExample();

int refValue = 5;
example.Ref(ref refValue);
Console.WriteLine($"Value after ref: {refValue}");

int outValue;
example.Out(out outValue);
Console.WriteLine($"Value after out: {outValue}");
}
}

Output:

Sign: ________________ Marks: ________________


10
12202040501079 Pushti Baldha
.NET Technology -202045604

PRACTICAL 04
4) Demonstrate the use of Properties and Indexer.
Code:
using System;
namespace prac4
{
public class Employee
{
private string _empName = "Not known";
private int _salary = 0;
private static int _cnt;

public Employee()
{
_cnt += 1;
}
public string EmpName
{
get { return _empName; }
set { _empName = value; }
}
public int Salary
{
get { return _salary; }
set { _salary = value; }
}
public static int Counter
{
get { return _cnt; }
}
}

public class ExampleDemo


{
static void Main(string[] args)
{
Employee e1 = new Employee();
e1.EmpName = "Pushti";
e1.Salary = 10000;
Console.WriteLine("Employee info: {0}", e1.EmpName);
Console.WriteLine("Employee salary: {0}", e1.Salary);
Console.WriteLine("Employee count: {0}", Employee.Counter);
Console.WriteLine();

Employee e2 = new Employee();


e2.EmpName = "Yogi";
e2.Salary = 5000;
Console.WriteLine("Employee info: {0}", e2.EmpName);
Console.WriteLine("Employee salary: {0}", e2.Salary);
Console.WriteLine("Employee count: {0}", Employee.Counter);
Console.WriteLine();

11
12202040501079 Pushti Baldha
.NET Technology -202045604

Employee e3 = new Employee();


e3.EmpName = "Dev";
e3.Salary = 800;
Console.WriteLine("Employee info: {0}", e3.EmpName);
Console.WriteLine("Employee salary: {0}", e3.Salary);
Console.WriteLine("Employee count: {0}", Employee.Counter);
Console.WriteLine();
}
}
}
Output:

12
12202040501079 Pushti Baldha
.NET Technology -202045604

4.2) Indexing
Code:
using System;
class Program
{

private string[] studentName = new string[10];


public string this[int index]
{
get
{
return studentName[index];
}
set
{
studentName[index] = value;
}
}

public static void Main()


{
Program obj = new Program();

obj[0] = "Pushti";
obj[1] = "Dev";
obj[2] = "Yogi";

Console.WriteLine("First element in obj: " + obj[0]);


Console.WriteLine("Second element in obj: " + obj[1]);
Console.WriteLine("Third element in obj: " + obj[2]);
}
}
Output:

Sign: ________________ Marks: ________________

13
12202040501079 Pushti Baldha
.NET Technology -202045604

PRACTICAL-05
Write a program for Arithmetic Calculator using Windows Application

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;

namespace Practical_5._1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int num1, num2;
string option;
int result;
string total;
private void textBox1_TextChanged(object sender, EventArgs e)
{
}

private void btn1_Click(object sender, EventArgs e)


{
Toaltext.Text = Toaltext.Text +"1";
}
private void btn2_Click(object sender, EventArgs e)
{
Toaltext.Text = Toaltext.Text + "2";
}
private void btn3_Click(object sender, EventArgs e)
{
Toaltext.Text = Toaltext.Text + "3";
}
private void btn4_Click(object sender, EventArgs e)
{
Toaltext.Text = Toaltext.Text + "4";
}
private void button5_Click(object sender, EventArgs e)
{
Toaltext.Text = Toaltext.Text + "5";

14
12202040501079 Pushti Baldha
.NET Technology -202045604

}
private void btn6_Click(object sender, EventArgs e)
{
Toaltext.Text = Toaltext.Text + "6";
}
private void button9_Click(object sender, EventArgs e)
{
Toaltext.Text = Toaltext.Text + "7";
}
private void button8_Click(object sender, EventArgs e)
{
Toaltext.Text = Toaltext.Text + "8";
}
private void button7_Click(object sender, EventArgs e)
{
Toaltext.Text = Toaltext.Text + "9";
}
private void btn0_Click(object sender, EventArgs e)
{
Toaltext.Text = Toaltext.Text + "0";
}
private void button15_Click(object sender, EventArgs e)
{
option = "+";
num1=int.Parse(Toaltext.Text);
Toaltext.Clear();
}
private void button14_Click(object sender, EventArgs e)
{
option = "-";
num1 = int.Parse(Toaltext.Text);
Toaltext.Clear();
}
private void button13_Click(object sender, EventArgs e)
{
option = "%";
num1 = int.Parse(Toaltext.Text);
Toaltext.Clear();
}
private void button12_Click(object sender, EventArgs e)
{
option = "*";
num1 = int.Parse(Toaltext.Text);
Toaltext.Clear();
}
private void button11_Click(object sender, EventArgs e)
{
option = "/";
num1 = int.Parse(Toaltext.Text);
Toaltext.Clear();
}

15
12202040501079 Pushti Baldha
.NET Technology -202045604

private void equal_Click(object sender, EventArgs e)


{
num2 = int.Parse(Toaltext.Text);
if (option == "+")
{
result = num1 + num2;
}
if (option == "-")
{
result = num1 - num2;
}
if (option == "*")
{
result = num1 * num2;
}
if (option == "/")
{
result = num1 / num2;
}
if (option == "%")
{
result = num1 % num2;
}
Toaltext.Text = result+"";
}
}
}
Output:
Adding 2 numbers:

16
12202040501079 Pushti Baldha
.NET Technology -202045604

Sign: _______________________ Marks: ____________________

17
12202040501079 Pushti Baldha
.NET Technology -202045604

PRACTICAL-6
Implement Windows Form based application using controls like menus,
dialog and tool tip, dropdown, radio and selection button etc.

Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PRACTICAL6
{
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
toolTip1.SetToolTip(button1, "Click to submit your registration");
}
private void label1_Click(object sender, EventArgs e)
{ }
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{ }
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{ }
private void label3_Click(object sender, EventArgs e)
{ }
private void button1_Click(object sender, EventArgs e)
{
string name = textBox1.Text;
string email = textBox2.Text;
string gender = radioButton1.Checked ? "Male" : radioButton2.Checked ?
"Female" : "Not selected";
string dept = listBox1.SelectedItem != null ? listBox1.SelectedItem.ToString() :
"Not selected";
string year = comboBox1.SelectedItem != null ? comboBox1.SelectedItem.ToString() :
"Not selected";
label6.Text = "Name: " + name;
label7.Text = "Email: " + email;
label8.Text = "Gender: " + gender;
label9.Text = "Dept: " + dept;

18
12202040501079 Pushti Baldha
.NET Technology -202045604

label10.Text = "Year: " + year;


}
}
}

Form2.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PRACTICAL6
{
public partial class Form2: Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
form.Show();
}
}
}

Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PRACTICAL6
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();

19
12202040501079 Pushti Baldha
.NET Technology -202045604

Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form2());
}
}
}
Output:

Sign: _______________________ Marks: ____________________

20
12202040501079 Pushti Baldha
.NET Technology -202045604

Practical-7
Implement concepts of Inheritance, visual inheritance and Interface in
widows form application.

Code:
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Practical7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public virtual void button1_Click(object sender, EventArgs e)


{
MessageBox.Show("Add Base button clicked");
}

public virtual void button2_Click(object sender, EventArgs e)


{
MessageBox.Show("Save Base Button clicked");
}
}
}
Form2.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Practical7
{
public partial class Form2 : Practical7.Form1
{

21
12202040501079 Pushti Baldha
.NET Technology -202045604

public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)


{

}
public override void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Add Dervied button clicked");
}

public override void button2_Click(object sender, EventArgs e)


{
MessageBox.Show("Save Derived Button clicked");
}
}
}
Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Practical7
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form2());
}
}
}

22
12202040501079 Pushti Baldha
.NET Technology -202045604

Sign: _______________________ Marks: ____________________

23
12202040501079 Pushti Baldha
.NET Technology -202045604

PRACTICAL – 8
Use Dataset, Data Reader, XML Reader & Data Sources (SQL, Object & XML)
with Any Windows or Web Application.
Form1.cs:
using System;
using System.Data;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace MySQLDemoApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string connectionString
="server=localhost;user=root;database=tithi;password=cow_@bark;";
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
try
{
conn.Open();
string query = "SELECT * FROM faculty";
MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn);
DataTable dt = new DataTable();
adapter.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// Optional: Handle cell click if needed
}
}
}

24
12202040501079 Pushti Baldha
.NET Technology -202045604

Form1.Design.cs:

namespace MySQLDemoApp
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGridView dataGridView1;

protected override void Dispose(bool disposing)


{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

private void InitializeComponent()


{
this.button1 = new System.Windows.Forms.Button();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(50, 50);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(100, 30);

25
12202040501079 Pushti Baldha
.NET Technology -202045604

this.button1.TabIndex = 0;
this.button1.Text = "View Data";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(180, 50);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowHeadersWidth = 51;
this.dataGridView1.RowTemplate.Height = 24;
this.dataGridView1.Size = new System.Drawing.Size(400, 250);
this.dataGridView1.TabIndex = 1;
this.dataGridView1.CellContentClick += new
System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClic
k);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(650, 350);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "MySQL Data Viewer";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
}
}

In MySql :

26
12202040501079 Pushti Baldha
.NET Technology -202045604

There is a table named “faculty”:

Output:

Sign: _______________________ Marks: ____________________

27
12202040501079 Pushti Baldha

You might also like