Net Framework and C# Programming Practical File
Net Framework and C# Programming Practical File
Net Framework and C# Programming Practical File
using System;
class Person
{
private string name; // Private field to store the name
// Property with a 'get' accessor to retrieve the name
public string GetName
{
get
{
return name;
}
}
// Property with a 'set' accessor to update the name
public string SetName
{
set
{
name = value;
}
}
}
class Program
{
static void Main()
{
Person person = new Person();
Output :
"Name: John Doe"
Practical 2
using System;
class StringUsingArray
{
private char[] characters;
public StringUsingArray(string input)
{
characters = new char[input.Length];
for (int i = 0; i < input.Length; i++)
{
characters[i] = input[i];
}
}
public override string ToString()
{
return new string(characters);
}
}
class Program
{
static void Main()
{
StringUsingArray myString = new StringUsingArray("Hello, .NET");
Output :
using System;
class ArmstrongNumbers
{
// Function to check if a number is an Armstrong number
static bool IsArmstrong(int num)
{
int originalNumber = num;
int numDigits = (int)Math.Floor(Math.Log10(num) + 1);
int sum = 0;
while (num > 0)
{
int digit = num % 10;
sum += (int)Math.Pow(digit, numDigits);
num /= 10;
}
return sum == originalNumber;
}
static void Main()
{
int start = 1;
int end = 1000;
Console.WriteLine("Armstrong numbers in the range from {0} to {1}:", start, end);
for (int i = start; i <= end; i++)
{
if (IsArmstrong(i))
{
Console.WriteLine(i);
}
}
}
}
Output :
4. Create a console application to calculate area of circle. Accept radius from user
Calculate circle area and print it Create a console application to build simple
calculator Calculator will have following functions Accept 2 numbers Perform
Add/Sub/Div/Mult Print Result.
using System;
class Program
{
static void Main()
{
Console.WriteLine("Welcome to the Circle Area Calculator and Simple Calculator");
Console.WriteLine("1. Calculate Circle Area");
Console.WriteLine("2. Simple Calculator");
Console.Write("Enter your choice (1/2): ");
if (int.TryParse(Console.ReadLine(), out int choice))
{
if (choice == 1)
{
Console.Write("Enter the radius of the circle: ");
if (double.TryParse(Console.ReadLine(), out double radius))
{
double area = Math.PI * Math.Pow(radius, 2);
Console.WriteLine("The area of the circle is: " + area);
}
else
{
Console.WriteLine("Invalid input. Please enter a valid number for the radius.");
}
}
else if (choice == 2)
{
Console.WriteLine("Simple Calculator");
Console.WriteLine("1. Add");
Console.WriteLine("2. Subtract");
Console.WriteLine("3. Multiply");
Console.WriteLine("4. Divide");
Console.Write("Enter your choice (1/2/3/4): ");
if (int.TryParse(Console.ReadLine(), out int calculatorChoice))
{
Console.Write("Enter the first number: ");
if (double.TryParse(Console.ReadLine(), out double num1))
{
Console.Write("Enter the second number: ");
if (double.TryParse(Console.ReadLine(), out double num2))
{
double result = 0.0;
switch (calculatorChoice)
{
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
if (num2 != 0)
result = num1 / num2;
else
Console.WriteLine("Error: Division by zero");
break;
default:
Console.WriteLine("Invalid choice. Please select 1, 2, 3, or 4.");
break;
}
Console.WriteLine("Result: " + result);
}
else
{
Console.WriteLine("Invalid input for the second number.");
}
}
else
{
Console.WriteLine("Invalid input for the first number.");
}
}
else
{
Console.WriteLine("Invalid choice. Please select 1, 2, 3, or 4.");
}
}
else
{
Console.WriteLine("Invalid choice. Please select 1 or 2.");
}
}
else
{
Console.WriteLine("Invalid choice. Please select 1 or 2.");
}
}
}
Output :
using System;
class Program
{
static void Main()
{
try
{
// Example 1: Divide by zero - Predefined exception
int numerator = 10;
int denominator = 0;
int result = numerator / denominator; // This will throw a System.DivideByZeroException
Console.WriteLine("Result: " + result); // This line will not be executed
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Predefined Exception: " + ex.Message);
}
try
{
// Example 2: Custom exception
int age = -5; // Negative age, which is not valid
if (age < 0)
{
throw new CustomException("Invalid age. Age cannot be negative.");
}
}
catch (CustomException ex)
{
Console.WriteLine("User-defined Exception: " + ex.Message);
}
Console.WriteLine("Program continues after exceptions.");
}
}
Output :
using System;
class Program
{
static void Main()
{
// Create an instance of the sealed class
Circle circle = new Circle(5.0);
double area = circle.CalculateArea();
// Attempt to create an instance of a derived class from the sealed class (commented out)
// DerivedCircle derivedCircle = new DerivedCircle();
}
}
Output :
using System;
using System.Data;
using System.Data.SqlClient;
class Program
{
static void Main()
{
// Connection string for your SQL Server database
string connectionString = "Data Source=YourServer;Initial Catalog=YourDatabase;User
ID=YourUser;Password=YourPassword";
Output :
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "sample.txt"; // Replace with the path to your text file
try
{
using (StreamReader reader = new StreamReader(filePath))
{
Console.WriteLine("Reading data from the file:");
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
Console.WriteLine(line);
}
}
}
catch (FileNotFoundException)
{
Console.WriteLine("File not found.");
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
"sample.txt" file
Hello, this is line 1.
This is line 2.
And here's line 3.
Output :
using System;
class Program
{
static void Main()
{
Publisher publisher = new Publisher();
Subscriber subscriber = new Subscriber();
Output :
10. Design the WEB base Database connectivity Form by using ASP.NET.
<connectionStrings>
<add name="MyDbConnection" connectionString="YourConnectionString" />
</connectionStrings>
Create an ASP.NET web page (e.g., "Default.aspx") and add the following code to the
.aspx file:
<!DOCTYPE html>
<html>
<head runat="server">
<title>Database Connectivity Form</title>
</head>
<body>
<form id="form1" runat="server">
<h1>Database Connectivity Form</h1>
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="True"></asp:GridView>
</div>
</form>
</body>
</html>
In the code-behind file (e.g., "Default.aspx.cs"), add the C# code to retrieve data from the
database and bind it to the GridView:
using System;
using System.Data;
using System.Data.SqlClient;
namespace WebDatabaseConnection
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
Replace "YourTable" with the name of the table you want to query.
Run the application, and you'll see a web page with a grid view that displays data
from your database.
Practical 11
using System;
class MyCollection
{
private string[] data = new string[5];
// Indexer
public string this[int index]
{
get
{
if (index >= 0 && index < data.Length)
return data[index];
else
return null; // Handle out-of-range index
}
set
{
if (index >= 0 && index < data.Length)
data[index] = value;
// Ignore if index is out of range
}
}
}
class Program
{
static void Main()
{
MyCollection collection = new MyCollection();
Output :