Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
42 views23 pages

Mca C#

The document is a certificate and instructional guide from the Cambridge Institute of Technology for Master of Computer Applications students, detailing various C# programming exercises. It includes programs demonstrating concepts such as boxing and unboxing, command line arguments, partial classes, access specifiers, inheritance, polymorphism, exception handling, ADO.NET, and ASP.NET controls. Each program is accompanied by code examples and expected output, serving as a practical resource for students in the MCA program.

Uploaded by

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

Mca C#

The document is a certificate and instructional guide from the Cambridge Institute of Technology for Master of Computer Applications students, detailing various C# programming exercises. It includes programs demonstrating concepts such as boxing and unboxing, command line arguments, partial classes, access specifiers, inheritance, polymorphism, exception handling, ADO.NET, and ASP.NET controls. Each program is accompanied by code examples and expected output, serving as a practical resource for students in the MCA program.

Uploaded by

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

CAMBRIDGE INSTITUTE OF TECHNOLOGY

An Autonomous Institution
KR Puram, Bangalore-560036

Department of Master of Computer Applications


Certificate
This is to certify that Mr. / Ms. bearing USN

No of MCA semester has satisfactorily completed

PCCL/IPCC laboratory during the academic year

Signature of Faculty Signature of HOD

Signature of Internal Examiner Signature of External Examiner


INDEX

Pg.No
SLNo Program

Program to demonstrate Boxing and Unboxing in C# 01


1

02
2 Program to accept and display Command Line Arguments in C#

03
3 Program using Partial Classes in C#

05
4 Program to demonstrate Extension Methods in C#

08
5 Program to demonstrate Access Specifiers in C#

10
6 Program to demonstrate Inheritance and Polymorphism in C#

12
Program to demonstrate Exception Handling in C#
7

13
8 ADO.NET program using DataReader, DataSet, DataAdapter, and DataView
16
9 ASP.NET webpage using Label, TextBox, and Button controls

18
10 ASP.NET Registration Form with Validation Controls
C# AND .NET Page 1

Program: 1
Write a Program in C# to demonstrate Boxing and unBoxing.

using System;
class BoxingUnboxingDemo
{
static void Main()
{
int num = 100; // Value type
object obj = num; // Boxing
Console.WriteLine("Boxed value: " + obj);

int unboxedNum = (int)obj; // Unboxing


Console.WriteLine("Unboxed value: " + unboxedNum);
}
}

OUTPUT

Boxed value: 100


Unboxed value: 100

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 2

Program: 2
Write a Program in C# to demonstrate Command line arguments processing

using System;
class CommandLineArgsDemo
{
static void Main(string[] args)
{
Console.WriteLine("Number of command line arguments: " + args.Length);

for (int i = 0; i < args.Length; i++)


{
Console.WriteLine("Argument[{0}] = {1}", i, args[i]);
}
}
}

OUTPUT

Number of command line arguments: 2


Argument[0] = apple
Argument[1] = banana

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 3

Program: 3
A. Write a C# programs to demonstrate the concepts of Partial classes.

// File: PartialClass1.cs
using System;
public partial class Person

{
public void SetName(string name)
{
this.Name = name;
}

public string Name { get; set; }


}

// File: PartialClass2.cs
using System;

public partial class Person


{
public void Display()
{
Console.WriteLine("Name: " + Name);
}
}
// File: Program.cs
using System;
class Program
{
static void Main()
{
Person p = new Person();

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 4

p.SetName("Alice");
p.Display();
}
}

OUTPUT
Name: Alice

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 5

Program: 3
B.Write a C# programs to demonstrate the concepts of Extension methods.

using System;
public static class ExtensionMethods
{
public static int WordCount(this string str)
{
return str.Split(' ', StringSplitOptions.RemoveEmptyEntries).Length;
}
}

class Program

{
static void Main()
{
string message = "Hello from extension method!";
int count = message.WordCount(); // Using the extension method
Console.WriteLine("Word Count: " + count);
}
}

OUTPUT

Word Count:4

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 6

Program: 4
Write a C# program to demonstrate the different access specifiers.

using System;
class AccessDemo
{
public int publicVar = 1;
private int privateVar = 2;
protected int protectedVar = 3;
internal int internalVar = 4;
protected internal int protectedInternalVar = 5;

public void Show()


{
Console.WriteLine("Inside AccessDemo:");
Console.WriteLine("publicVar = " + publicVar);
Console.WriteLine("privateVar = " + privateVar);
Console.WriteLine("protectedVar = " + protectedVar);
Console.WriteLine("internalVar = " + internalVar);
Console.WriteLine("protectedInternalVar = " + protectedInternalVar);
}
}

class DerivedDemo : AccessDemo


{
public void ShowDerived()
{
Console.WriteLine("Inside DerivedDemo:");
Console.WriteLine("publicVar = " + publicVar);
// Console.WriteLine("privateVar = " + privateVar); // Not accessible
Console.WriteLine("protectedVar = " + protectedVar);
Console.WriteLine("internalVar = " + internalVar);
Console.WriteLine("protectedInternalVar = " + protectedInternalVar);

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 7

}
}

class Program
{
static void Main()
{
AccessDemo obj = new AccessDemo();
obj.Show();

DerivedDemo d = new DerivedDemo();


d.ShowDerived();

Console.WriteLine("Outside class:");
Console.WriteLine("publicVar = " + obj.publicVar);
// Console.WriteLine("privateVar = " + obj.privateVar); // Not accessible
// Console.WriteLine("protectedVar = " + obj.protectedVar); // Not accessible
Console.WriteLine("internalVar = " + obj.internalVar);
Console.WriteLine("protectedInternalVar = " + obj.protectedInternalVar);
}
}

OUTPUT

Inside AccessDemo:
publicVar = 1
privateVar = 2
protectedVar = 3
internalVar = 4
protectedInternalVar = 5
Inside DerivedDemo:
publicVar = 1
protectedVar = 3

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 8

internalVar = 4
protectedInternalVar = 5
Outside class:
publicVar = 1
internalVar = 4
protectedInternalVar = 5

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 9

Program: 5
Write a C# programs to demonstrate the concepts of Constructors and Inheritance.

using System;
class Animal
{
public string Name;

// Constructor
public Animal(string name)
{
Name = name;
Console.WriteLine("Animal constructor called");
}

public void Display()


{
Console.WriteLine("Animal Name: " + Name);
}
}

// Derived class
class Dog : Animal

{
public string Breed;

// Constructor using base keyword


public Dog(string name, string breed) : base(name)
{
Breed = breed;
Console.WriteLine("Dog constructor called");
}

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 10

public void ShowDetails()


{
Console.WriteLine($"Dog Name: {Name}, Breed: {Breed}");
}
}

class Program
{
static void Main()
{
Dog myDog = new Dog("Buddy", "Golden Retriever");
myDog.ShowDetails();
}
}

OUTPUT

Animal constructor called


Dog constructor called
Dog Name: Buddy, Breed: Golden Retriever

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 11

Program: 6
Write a C# programs to demonstrate the concepts of Polymorphism.
using System;
class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing a shape");
}
}
class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a circle");
}
}

class Rectangle : Shape


{
public override void Draw()
{
Console.WriteLine("Drawing a rectangle");
}
}

class Program
{
static void Main()
{
Shape s1 = new Circle(); // Runtime polymorphism
Shape s2 = new Rectangle();

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 12

s1.Draw(); // Outputs: Drawing a circle


s2.Draw(); // Outputs: Drawing a rectangle
}
}

OUTPUT

Drawing a circle
Drawing a rectangl

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 13

Program: 7
Using Try, Catch and Finally blocks write a program in C# to demonstrate error handling.
using System;
class Program
{
static void Main()
{
try
{
Console.Write("Enter a number: ");
int num = Convert.ToInt32(Console.ReadLine());

int result = 100 / num; // Might throw DivideByZeroException


Console.WriteLine("Result: " + result);
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Error: Cannot divide by zero.");
}
catch (FormatException ex)
{
Console.WriteLine("Error: Invalid input. Please enter a number.");

}
finally
{
Console.WriteLine("This block always executes (finally block).");
}
}
OUTPUT
Enter a number: 0
Error: Cannot divide by zero.
This block always executes (finally block).

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 14

Program: 8
Create an ADO.NET applications in C# to demonstrate the DataReader, DataSet,
DataAdapter and DataView Objects.

Table Creation SQL

CREATE TABLE Customers (


CustomerID INT PRIMARY KEY,
Name VARCHAR(100),
City VARCHAR(100)
);
INSERT INTO Customers VALUES (1, 'John Doe', 'New York'), (2, 'Jane Smith', 'Los Angeles'),
(3, 'Mike Brown', 'Chicago');

C# Code (ADO.NET Demo)


using System;
using System.Data;
using System.Data.SqlClient;

namespace AdoNetDemo
{
class Program
{
static void Main(string[] args)
{
string connectionString =
@"Server=.\SQLEXPRESS;Database=YourDatabaseName;Trusted_Connection=True;";

// DataReader Example
Console.WriteLine("=== DataReader Example ===");
using (SqlConnection con = new SqlConnection(connectionString))

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 15

{
con.Open();

SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con);


SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine($"ID: {reader["CustomerID"]}, Name: {reader["Name"]}, City:
{reader["City"]}");
}
reader.Close();
}

// DataSet and DataAdapter Example


Console.WriteLine("\n=== DataSet and DataAdapter Example ===");
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Customers", con);
DataSet ds = new DataSet();
adapter.Fill(ds, "Customers");

foreach (DataRow row in ds.Tables["Customers"].Rows)


{
Console.WriteLine($"ID: {row["CustomerID"]}, Name: {row["Name"]}, City:
{row["City"]}");
}

// DataView Example
Console.WriteLine("\n=== DataView Example (Filtered by City = 'Chicago') ===");
DataView dv = new DataView(ds.Tables["Customers"]);
dv.RowFilter = "City = 'Chicago'";

foreach (DataRowView rowView in dv)

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 16

{
Console.WriteLine($"ID: {rowView["CustomerID"]}, Name: {rowView["Name"]},
City: {rowView["City"]}");
}
}
}
}
}

OUTPUT

=== DataReader Example ===


ID: 1, Name: John Doe, City: New York
ID: 2, Name: Jane Smith, City: Los Angeles
ID: 3, Name: Mike Brown, City: Chicago

=== DataSet and DataAdapter Example ===


ID: 1, Name: John Doe, City: New York
ID: 2, Name: Jane Smith, City: Los Angeles
ID: 3, Name: Mike Brown, City: Chicago

=== DataView Example (Filtered by City = 'Chicago') ===


ID: 3, Name: Mike Brown, City: Chicago

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 17

Program: 9
Design an ASP.NET Webpage to demonstrate the Label, Button and Textbox controls.

// Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Label, TextBox and Button Demo</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblMessage" runat="server" Text="Enter your name: "></asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"
/>
<br /><br />
<asp:Label ID="lblOutput" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>

//Default.aspx.cs
using System;
public partial class _Default : System.Web.UI.Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 18

lblOutput.Text = "Hello, " + txtName.Text + "!";


}
}

OUTPUT

Hello, Alice!

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 19

Program: 10

Develop a Registration Form with all Validation Controls.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Registration.aspx.cs"


Inherits="Registration" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Registration Form</title>
</head>
<body>

<form id="form1" runat="server">


<h2>Registration Form</h2>
<div>
<asp:Label ID="lblName" runat="server" Text="Name: " />
<asp:TextBox ID="txtName" runat="server" />
<asp:RequiredFieldValidator ID="rfvName" runat="server"
ControlToValidate="txtName"
ErrorMessage="Name is required." ForeColor="Red" />
<br /><br />
<asp:Label ID="lblEmail" runat="server" Text="Email: " />
<asp:TextBox ID="txtEmail" runat="server" />
<asp:RequiredFieldValidator ID="rfvEmail" runat="server"
ControlToValidate="txtEmail"
ErrorMessage="Email is required." ForeColor="Red" />
<asp:RegularExpressionValidator ID="revEmail" runat="server"
ControlToValidate="txtEmail"
ErrorMessage="Invalid email format."
ValidationExpression="^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$" ForeColor="Red" />
<br /><br />

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 20

<asp:Label ID="lblPassword" runat="server" Text="Password: " />


<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" />
<asp:RequiredFieldValidator ID="rfvPassword" runat="server"
ControlToValidate="txtPassword"
ErrorMessage="Password is required." ForeColor="Red" />
<asp:CompareValidator ID="cvPassword" runat="server"
ControlToValidate="txtPassword"
ControlToCompare="txtConfirmPassword" ErrorMessage="Passwords must match."
ForeColor="Red" />
<br /><br />
<asp:Label ID="lblConfirm" runat="server" Text="Confirm Password: " />
<asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password" />
<asp:RequiredFieldValidator ID="rfvConfirmPassword" runat="server"
ControlToValidate="txtConfirmPassword"
ErrorMessage="Confirmation is required." ForeColor="Red" />
<br /><br />
<asp:Button ID="btnRegister" runat="server" Text="Register"
OnClick="btnRegister_Click" />
<br /><br />
<asp:Label ID="lblResult" runat="server" />
</div>
</form>
</body>
</html>

// Registration.aspx.cs
using System;

public partial class Registration : System.Web.UI.Page


{
protected void btnRegister_Click(object sender, EventArgs e)
{
if (Page.IsValid)

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25


C# AND .NET Page 21

lblResult.Text = "Registration Successful!";


lblResult.ForeColor = System.Drawing.Color.Green;
}
}

OUTPUT
1. If fields are left empty:
Displays validation error messages in red:
Name is required.
Email is required.
Password is required.
Confirmation is required.
2. If email is invalid:
Invalid email format.
3. If passwords don’t match:
Passwords must match.
4. If all fields are valid:
• User enters:
o Name: Tom
o Email: [email protected]
o Password: 12345
o Confirm Password: 12345
• Clicks Register
Expected Web Output:
Registration Successful!

CAMBRIDGE INSTITUTE OF TECHNOLOGY MCA 2024-25

You might also like