C#
C# and .Net Framework Lab
SNJPSNMS Degree College, Nidasoshi 1
Program 1: Write c# program to show the machine details like machine name,
operating system, version, physical memory and calculate the time since last
boot up.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("machine name is:"+Environment.MachineName);
Console.WriteLine("\ncurrent operating systeem is:"+Environment.OSVersion);
Console.WriteLine("\nexecuting version of visual studio .net is"+Environment.Version);
Console.WriteLine("\ncurrent directory is:"+Environment.CurrentDirectory);
Console.WriteLine("\nyou have{0} byes of RAM in your system", +
Environment.WorkingSet);
Console.WriteLine("\nthe system time since last bootup is:{0} minutes",
(Environment.TickCount)/1000/60);
Console.WriteLine("\nlogical drives in the system are :");
String[] drives=Environment.GetLogicalDrives();
for (int i = 0; i < drives.Length; i++)
Console.WriteLine("drives{0}:{1}",i, drives[i]);
Console.ReadKey();
}
}
C# and .Net Framework Lab
SNJPSNMS Degree College, Nidasoshi 1
Program 1: Write c# program to show the machine details like machine name,
operating system, version, physical memory and calculate the time since last
boot up.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("machine name is:"+Environment.MachineName);
Console.WriteLine("\ncurrent operating systeem is:"+Environment.OSVersion);
Console.WriteLine("\nexecuting version of visual studio .net is"+Environment.Version);
Console.WriteLine("\ncurrent directory is:"+Environment.CurrentDirectory);
Console.WriteLine("\nyou have{0} byes of RAM in your system", +
Environment.WorkingSet);
Console.WriteLine("\nthe system time since last bootup is:{0} minutes",
(Environment.TickCount)/1000/60);
Console.WriteLine("\nlogical drives in the system are :");
String[] drives=Environment.GetLogicalDrives();
for (int i = 0; i < drives.Length; i++)
Console.WriteLine("drives{0}:{1}",i, drives[i]);
Console.ReadKey();
}
}
C# and .Net Framework Lab
SNJPSNMS Degree College, Nidasoshi 5
Program 3: Write a program in c# to create a function to calculate the sum of
the individual digits of a given number.
using System;
class Program
{
static void Main(string[] args)
{
int num;
Console.WriteLine("enter a number");
num=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("the sum of the digits of the number {0} is:{1}\n", num,
SumCal(num));
Console.ReadLine();
}
public static int SumCal(int number)
{
String n1=Convert.ToString(number);
int sum=0;
for(int i=0;i<n1.Length;i++)
sum+=Convert.ToInt32(n1.Substring(i,1));
return sum;
Console.ReadLine();
}
}
C# and .Net Framework Lab
SNJPSNMS Degree College, Nidasoshi 7
Program 4: Draw a square with side 100fixels in length. Then inscribe (draw)
a circle of radius 50 inside the square. Position the square and inscribed circle
in the middle of the screen.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace progr4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Pen Redpen = new Pen(Color.Red, 6);
Pen Bluepen = new Pen(Color.Blue, 4);
Pen Blackpen = new Pen(Color.Black, 2);
Rectangle rect = new Rectangle(80, 80, 50, 50);
//draw ellipses
e.Graphics.DrawEllipse(Blackpen, 100.0F, 100.0F, 10.0F, 10.0F);
e.Graphics.DrawRectangle(Redpen, rect);
e.Graphics.DrawEllipse(Bluepen,60,60,90,90);
e.Graphics.DrawRectangle(Blackpen,40.0F,40.0F,130.0F,130.0F);
//dispose of objects
Redpen.Dispose();
Blackpen.Dispose();
Bluepen.Dispose();
}
}
}
C# and .Net Framework Lab
SNJPSNMS Degree College, Nidasoshi 9
Program 5: Write a program to implement multi-level inheritance.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace program5
{
class son : Father
{
public void DisplayTwo()
{
Console.WriteLine("son..");
}
static void main(String[] args)
{
son s = new son();
s.Display();
s.DisplayOne();
s.DisplayTwo();
Console.Read();
}
}
class Grandfather
{
public void Display()
{
Console.WriteLine("grandfather...");
}
}
class Father : Grandfather
{
public void DisplayOne()
{
Console.WriteLine("Father...");
}
}
}
C# and .Net Framework Lab
SNJPSNMS Degree College, Nidasoshi 11
Program 6: Write a c# program to demonstrate system exception.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace pro6
{
class Program
{
static void Main(string[] args)
{
try
{
int[] arr = new int[5];
arr[10] = 25;
}
catch (SystemException e)
{
Console.WriteLine(e);
}
Console.ReadKey();
}
}
}
C# and .Net Framework Lab
SNJPSNMS Degree College, Nidasoshi 12
Program 7: Write an Object- oriented program to demonstrate bank transaction include
methods for amount deposit, withdraw, display.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace program7
{
class bank
{
public int bal = 500;
public void deposit()
{
int depositamt;
Console.WriteLine("enter the amount to be deposit:");
depositamt = int.Parse(Console.ReadLine());
bal = bal + depositamt;
Console.WriteLine("your current balance is:" + bal);
}
public void withdraw()
{
int withdrawamt;
Console.WriteLine("enter the amount to be withdraw:");
withdrawamt = int.Parse(Console.ReadLine());
if (withdrawamt > bal)
{
Console.WriteLine("in sufficient amount");
}
else
bal = bal - withdrawamt;
Console.WriteLine("your current balance is:" + bal);
}
}
class program
{
static void Main(String[] args)
{
bank b = new bank();
b.deposit();
b.withdraw();
Console.ReadLine();
}
}
}
C# and .Net Framework Lab
SNJPSNMS Degree College, Nidasoshi 14
Program 8: Demonstrate operator overloading using two complex numbers.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace program8
{
class Program
{
static void Main(string[] args)
{
Complex c1 = new Complex(3, 7);
c1.Display();
Complex c2 = new Complex(5, 2);
c2.Display();
Complex c3 = c1 + c2;
c3.Display();
Console.ReadKey();
}
}
public class Complex
{
private int real;
private int img;
public Complex(int r=0,int i=0)
{
real =r;
img =i;
}
public static Complex operator +(Complex c1, Complex c2)
{
Complex temp = new Complex();
temp.real = c1.real + c2 .real;
temp.img = c1.img + c2. img;
return temp;
}
public void Display()
{
Console.WriteLine("{0} + i{1}",real,img);
}
}
}
C# and .Net Framework Lab
SNJPSNMS Degree College, Nidasoshi 16
Program 9: Demonstrate DialogBoxes (Modal and Modeless)
Form1.cs [Design]
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.Windows.Forms;
namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
public static Control form1;
public Form1()
{
InitializeComponent();
}
C# and .Net Framework Lab
SNJPSNMS Degree College, Nidasoshi 17
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
form1 = this;
f2.Show();
f2.textBox1.Text = this.textBox1.Text;
}
}
}
Form2.cs [Design]
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.Windows.Forms;
C# and .Net Framework Lab
SNJPSNMS Degree College, Nidasoshi 18
namespace WindowsFormsApplication9
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < Form1.form1.Controls.Count; i++)
{
if ((Form1.form1.Controls[i] is TextBox) && (Form1.form1.Controls[i].Name == "textBox1"))
{
Form1.form1.Controls[i].Text = this.textBox1.Text;
break;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
C# and .Net Framework Lab
SNJPSNMS Degree College, Nidasoshi 20
Program 10: Write a program in c# to create menu and menuitems.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace program10
{
public partial class Form1 : Form
{
private MainMenu mainMenu;
public Form1()
{
InitializeComponent();
mainMenu =new MainMenu ();
MenuItem File = mainMenu.MenuItems.Add("&File");
File.MenuItems.Add(new MenuItem("&new"));
File.MenuItems.Add(new MenuItem("&open"));
File.MenuItems.Add(new MenuItem("&exit"));
this.Menu =mainMenu;
MenuItem about=mainMenu.MenuItems.Add("&about");
about.MenuItems.Add(new MenuItem("&about"));
this.Menu =mainMenu;
mainMenu.GetForm().BackColor =Color.Indigo;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Comments
Post a Comment