0% found this document useful (0 votes)
79 views29 pages

Kunal Kumar: 03911804413 MCA 3rd Sem

This document contains lab exercises and questions on C# .NET programming. It covers topics such as creating executable and DLL files in Visual Studio, using the CSC compiler, abstract classes and inheritance, interfaces, delegates, dictionaries, IComparable, and IEnumerable. For each topic, code examples are provided as answers to the questions.

Uploaded by

kuku288
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)
79 views29 pages

Kunal Kumar: 03911804413 MCA 3rd Sem

This document contains lab exercises and questions on C# .NET programming. It covers topics such as creating executable and DLL files in Visual Studio, using the CSC compiler, abstract classes and inheritance, interfaces, delegates, dictionaries, IComparable, and IEnumerable. For each topic, code examples are provided as answers to the questions.

Uploaded by

kuku288
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/ 29

C#.

Net Programming
Kunal Kumar
03911804413
MCA 3rd sem

INDEX
S.NO.

TOPIC

DATE

SIGN

Lab Exercise :- 1
Ques. Develop an exe with Visual Studio.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace proj3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("This is the simple executable File ");
Console.ReadKey();
}
}
}

Lab Exercise :- 2
Ques. Develop a private dll with Visual Studio.
using
using
using
using
using

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

namespace MyLib1
{
public class MyClass
{
public static void display(String s)
{
Console.WriteLine("Hello .."+s+"This is the Display Function of MyClass of MyLib1.dll");
}
}
}

Use the created dll classes in our file


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace console
{
class Program
{
static void Main(string[] args)
{
MyLib1.MyClass.display("KuKu");
Console.WriteLine("Main Exited");
}
}}

Lab Exercise :- 3
Ques. Develop shared dll using Visual Studio.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
[assembly: AssemblyKeyFile("D:\\programs\\Dot Net\\key1.snk")]
namespace MyLib1
{
public class MyClass
{
public static void display(String s)
{
Console.WriteLine("Hello .."+s+"This is the Display Function of MyClass of MyLib1.dll");
}
}
}

Key is created for strong name of MyLib1.dll

Lab Exercise :- 4
Ques. Write a program in notepad and compile it using CSC compiler.

using System;
namespace Outer
{
public class MyClass
{
static void Main()
{Console.WriteLine("Main Method of My Class");}
}

LAB EXERCISE-5
Ques. Create an abstract class shape with length, width, height and radius as fields and Area() as
abstract function and inherited it for Circle, Square and Rectangle class.
[Develop this program in ASP.NET with good look and feel. Use Images wherever required].

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"


AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<Table Border="2" width="700Px">
<tr>
<td class="style1"><asp:Image runat="server" ID="ImgCircle" ImageUrl="~/circle.jpg">
</asp:Image> </td>
<td><asp:Label runat="server" ID="LabRadius" Text="Radius"></asp:Label></td>
<td><asp:TextBox runat="server" ID="TxtRadius"></asp:TextBox></td>
<td>Area:<asp:Label runat="server" ID="LabCircleArea"></asp:Label></td>
<td><asp:Label ID="area1" runat="server" Text=""></asp:Label></td>
</tr>
<tr>
<td class="style1"><asp:Image runat="server" ID="ImgSquare" ImageUrl="~/square.png">
</asp:Image> </td>
<td><asp:Label runat="server" ID="LabSize" Text="Size"></asp:Label></td>
<td><asp:TextBox runat="server" ID="TxtSize"></asp:TextBox></td>
<td>Area:<asp:Label runat="server" ID="LabSquareArea"></asp:Label></td>
<td><asp:Label ID="area2" runat="server" Text=""></asp:Label></td>
</tr>
<tr>
<td class="style1"><asp:Image runat="server" ID="Image1" ImageUrl="~/rectangle.png">
</asp:Image> </td>
<td><asp:Label runat="server" ID="LabLength" Text="Length"></asp:Label><br/>
<asp:Label runat="server" ID="LabWidth" Text="Width"></asp:Label>
</td>
<td><asp:TextBox runat="server" ID="TxtLength"></asp:TextBox><br/>
<asp:TextBox runat="server" ID="TxtWidth"></asp:TextBox>
</td>
<td>Area:<asp:Label runat="server" ID="LabRectArea"></asp:Label></td>
<td><asp:Label ID="area3" runat="server" Text=""></asp:Label></td>
</tr>
<tr><td align="center" colspan="4"><asp:Button runat="server" ID="BtnSubmit"
Text="Submit" onclick="BtnSubmit_Click"> </asp:Button>
</td> </tr>
</Table>
</asp:Content>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{ public abstract class Shape
{
protected float length;
protected float width;
protected float height;
protected float radius;
abstract public float area();
}
class Circle : Shape
{public Circle(float r)
{
radius = r;
} public override float area()
{
return (3.14f * radius * radius);
}
}
class Square : Shape
{
public Square(float h)
{
height = h;
}
public override float area()
{
return (height * height);
}
}
class Rectangle : Shape
{
public Rectangle(float l, float w)
{length = l;
width = w;
}
public override float area()
{
return (length * width);
}
}

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


{
protected void BtnSubmit_Click(object sender, EventArgs e)
{
if ((TxtRadius.Text) != "")
{
float r = (float)Convert.ToDecimal(TxtRadius.Text);
Circle cobj = new Circle(r);
area1.Text = Convert.ToString(cobj.area());
}
if ((TxtSize.Text) != "")
{
float s = (float)Convert.ToDecimal(TxtSize.Text);
Square sobj = new Square(s);
area2.Text = Convert.ToString(sobj.area());
}
if ((TxtLength.Text) != "" && (TxtRadius.Text) != "")
{
float l = (float)Convert.ToDecimal(TxtLength.Text);
float b = (float)Convert.ToDecimal(TxtWidth.Text);
Rectangle robj = new Rectangle(l, b);
area3.Text = Convert.ToString(robj.area());
} } } }

Ques. Demonstrate the Explicit Interface Implementation by creating two or more interfaces that
have same signatures.

using System;
interface Italk1
{
void Read();
}
interface Italk2
{
void Read();
}
class Derived1:Italk1,Italk2
{
void Italk1.Read()
{
Console.WriteLine("Reading from First Interface");
}
void Italk2.Read()
{
Console.WriteLine("Reading from Second Interface");
}
public static void Main(string[] args)
{
Derived dobj=new Derived();
Italk1 o1=(Italk1)dobj;
Italk2 o2=(Italk2)dobj;
o1.Read();
o2.Read();
Console.ReadKey();
}
}

Ques. Demonstrate the Explicit Interface Implementation by creating two or more interface that
have same signatures with Operator AS.
using System;
interface Italk1
{
void Read();
}
interface Italk2
{
void Read();
}
class Derived:Italk1,Italk2
{
void Italk1.Read()
{
Console.WriteLine("Reading from First interface");
}
void Italk2.Read()
{
Console.WriteLine("Reading from Second Interface");
}
public static void Main(string[] args)
{
Derived1 dobj=new Derived1();
Italk1 o1=dobj as Italk1;
Italk2 o2=dobj as Italk2;
o1.Read();
o2.Read();
Console.ReadKey();
}
}

LAB EXERCISE - 6
Ques. Write a program in C# to implement Delegates.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public delegate decimal Calculation(decimal a1, decimal a2);
namespace delegate1
{
class Program
{
static void Main(string[] args)
{
Calculation obj=new Calculation(add);
Console.WriteLine("Add Func "+obj(20.0m,22.0m));
obj=new Calculation(sub);
Console.WriteLine("Sub Func "+obj(20.0m,22.0m));
Console.ReadKey();
}
public static decimal add(decimal v1, decimal v2)
{
return v1 + v2;
}
public static decimal sub(decimal v1, decimal v2)
{
return v1 - v2;
}
}
}

Ques. Write a program to implement Multicast Deligate.


using System;
public delegate void Notifier(string sender);
public class MultiDelegateExample
{
Notifier greetings;
public void SayHello(string name)
{
Console.WriteLine(" Hello from " + name);
}
public void SayGoodBye(string name)
{
Console.WriteLine(" Good bye from " + name);
}
public static void Main(string[] args)
{
MultiDelegateExample del = new MultiDelegateExample();
del.greetings = new Notifier(del.SayHello);
del.greetings += new Notifier(del.SayGoodBye);
del.greetings("Kunal");
Console.Read();
}
}

Ques. Write a program to demonstrate the concept of dictionary<key type,value type>pair.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DictionarySample
{
class Program1
{
static void Main(string[] args)
{
Dictionary<string, Int16> weekdays= new Dictionary<string, Int16>();
weekdays.Add("monday", 1);
weekdays.Add("tuesday", 2);
weekdays.Add("wednesday", 3);
weekdays.Add("thursday", 4);
weekdays.Add("friday", 5);
weekdays.Add("saturday", 6);
weekdays.Add("sunday", 7);

Console.WriteLine("week days List");


foreach( KeyValuePair<string, Int16> week in weekdays )
{
Console.WriteLine("Key = {0}, Value = {1}",
week.Key, week.Value);
}
Console.ReadKey();
}
}
}

Ques. Write a program to implement Icomparable.


using System;
using System.Collections;
// a simple class to store in the array
public class Employee : IComparable
{
public Employee(int empID)
{
this.empID = empID;
}
public override string ToString( )
{
return empID.ToString( );
}
// Comparer delegates back to Employee
// Employee uses the integer's default
// CompareTo method
public int CompareTo(Object rhs)
{
Employee r = (Employee) rhs;
return this.empID.CompareTo(r.empID);
}
private int empID;
}

public class Tester


{
static void Main( )
{
ArrayList empArray = new ArrayList( );
ArrayList intArray = new ArrayList( );
// generate random numbers for
// both the integers and the
// employee id's
Random r = new Random( );
// populate the array
for (int i = 0;i<5;i++)
{
// add a random employee id
empArray.Add(new Employee(r.Next(10)+100));
// add a random integer
intArray.Add(r.Next(10));
}
// display all the contents of the int array
for (int i = 0;i<intArray.Count;i++)
{
Console.Write("{0} ", intArray[i].ToString( ));
}
Console.WriteLine("\n");
// display all the contents of the Employee array
for (int i = 0;i<empArray.Count;i++)
{
Console.Write("{0} ", empArray[i].ToString( ));
}
Console.WriteLine("\n");
// sort and display the int array
intArray.Sort( );
for (int i = 0;i<intArray.Count;i++)
{
Console.Write("{0} ", intArray[i].ToString( ));
}
Console.WriteLine("\n");
// sort and display the employee array
//Employee.EmployeeComparer c = Employee.GetComparer( );
//empArray.Sort(c);
empArray.Sort( );
// display all the contents of the Employee array
for (int i = 0;i<empArray.Count;i++)
{

Console.Write("{0} ", empArray[i].ToString( ));


}
Console.WriteLine("\n");
}
}

Ques. Write a program to implement the IEnumerable Interface.


using System;
using System.Collections;
// a simplified ListBox control
public class ListBoxTest : IEnumerable
{
// private implementation of ListBoxEnumerator
private class ListBoxEnumerator : IEnumerator
{
private ListBoxTest lbt;
private int index;
// public within the private implementation
// thus, private within ListBoxTest
public ListBoxEnumerator(ListBoxTest lbt)
{
this.lbt = lbt;
index = -1;
}
// Increment the index and make sure the
// value is valid
public bool MoveNext( )

{
index++;
if (index >= lbt.strings.Length)
return false;
else
return true;
}
public void Reset( )
{
index = -1;
}
// Current property defined as the
// last string added to the listbox
public object Current
{
get
{
return(lbt[index]);
}
}

}
// Enumerable classes can return an enumerator
public IEnumerator GetEnumerator( )
{
return (IEnumerator) new ListBoxEnumerator(this);
}
// initialize the list box with strings
public ListBoxTest(params string[] initialStrings)
{
// allocate space for the strings
strings = new String[8];
// copy the strings passed in to the constructor
foreach (string s in initialStrings)
{
strings[ctr++] = s;
}
}
// add a single string to the end of the list box
public void Add(string theString)

{
strings[ctr] = theString;
ctr++;
}
// allow array-like access
public string this[int index]
{
get
{
if (index < 0 || index >= strings.Length)
{
// handle bad index
}
return strings[index];
}
set
{
strings[index] = value;
}
}
// publish how many strings you hold
public int GetNumEntries( )
{
return ctr;
}
private string[] strings;
private int ctr = 0;
}
public class Tester
{
static void Main( )
{
// create a new list box and initialize
ListBoxTest lbt =
new ListBoxTest("Hello", "World");
// add a few strings
lbt.Add("Who");
lbt.Add("Is");
lbt.Add("smith");
lbt.Add("walker");
// test the access
string subst = "Universe";
lbt[1] = subst;

// access all the strings


foreach (string s in lbt)
{
Console.WriteLine("Value: {0}", s);
}
}
}

Ques. Write a program to implement generic Queue.


using System;
using System.Collections;
namespace CollectionsApplication
{
class genericqueue
{
static void Main(string[] args)
{
Queue q = new Queue();
q.Enqueue('H');
q.Enqueue('e');
q.Enqueue('l');
q.Enqueue('l');
q.Enqueue('o');
q.Enqueue('o');
Console.WriteLine("Current queue: ");
foreach (char c in q)

Console.Write(c + " ");


Console.WriteLine();
q.Enqueue(' ');
q.Enqueue('b');
q.Enqueue(' ');
q.Enqueue('y');
q.Enqueue('e');
q.Enqueue('e');
q.Enqueue('e');
Console.WriteLine("Current queue: ");
foreach (char c in q)
Console.Write(c + " ");
Console.WriteLine();
Console.WriteLine("Removing some values ");
char ch = (char)q.Dequeue();
Console.WriteLine("The removed value: {0}", ch);
ch = (char)q.Dequeue();
Console.WriteLine("The removed value: {0}", ch);
ch = (char)q.Dequeue();
Console.WriteLine("The removed value: {0}", ch);
ch = (char)q.Dequeue();
Console.WriteLine("The removed value: {0}", ch);
ch = (char)q.Dequeue();
Console.WriteLine("The removed value: {0}", ch);
ch = (char)q.Dequeue();
Console.WriteLine("The removed value: {0}", ch);
Console.WriteLine("Current queue: ");
foreach (char c in q)
Console.Write(c + " ");
Console.ReadKey();
}
}
}

Ques. Write a program to implement a generic stack.


using System;
using System.Collections;
namespace CollectionsApplication
{
class genericstack
{
static void Main(string[] args)
{
Stack st = new Stack();
st.Push('o');
st.Push('l');
st.Push('l');
st.Push('e');
st.Push('h');
Console.WriteLine("Current stack: ");
foreach (char c in st)
{
Console.Write(c + " ");
}
Console.WriteLine();
Console.WriteLine("Removing values ");
st.Pop();
st.Pop();
st.Pop();

st.Pop();
Console.WriteLine("Current stack: ");
foreach (char c in st)
{
Console.Write(c + " ");
Console.ReadKey();
}
}
}
}

Ques. Write a program to demonstrate the Lambda expression.


using System;
using System.Collections.Generic;
class Lambda
{
static void Main()
{
List<int> elements = new List<int>() { 10, 20, 31, 40 };
// ... Find index of first odd element.
int oddIndex = elements.FindIndex(x => x % 2 != 0);
Console.Write("The index of first odd position is: ");
Console.WriteLine(oddIndex);
}
}

Ques. Write a program using func<> concept.


using System;
public class GenericFunc
{
public static void Main()
{
// Instantiate delegate to reference UppercaseString method
Func<string, string> convertMethod = UppercaseString;
string name = "Tiger Shroff";
// Use delegate instance to call UppercaseString method
Console.WriteLine(convertMethod(name));
}
private static string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
}

Ques. Write a program to demonstrate the Action<t> Delegate.


using System;
public class Actionfunc
{
public static void Main()
{
Action<string> string1 = x => Console.WriteLine(x);

string1("CDAC");
}
}

Ques. Write a program to demonstrate the concept of predicate<>.


using System;
class Predicatefunc
{
static void Main()
{
Predicate<int> isOne =
x => x == 1;
Predicate<int> isGreaterEqualFive =
(int x) => x >= 5;
Console.WriteLine(isOne.Invoke(1));
Console.WriteLine(isOne.Invoke(2));
Console.WriteLine(isGreaterEqualFive.Invoke(3));
Console.WriteLine(isGreaterEqualFive.Invoke(10));
}
}

LAB EXERCISE - 7
Ques. Convert an XML document to XSLT.
XML DOCUMENT
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
<cd country="USA">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<price>10.95</price>
</cd>
<cd country="UK">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<price>10.0</price>
</cd>
<cd country="USA">
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<price>9.90</price>
</cd>
<cd country="USA">
<title>Empire Burlesque</title>
<artist>Ankit</artist>
<price>10.95</price>
</cd>
<cd country="India">
<title>Greatest Hits</title>
<artist>ABC</artist>
<price>8</price>
</cd>
</catalog>

XSLT
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>

<table border="1">
<xsl:for-each select="catalog/cd">
<tr>
<td>TITLE: <xsl:value-of select="Name"/>
<br/>Artist:<xsl:value-of select="artist"/>
<br/>Price<xsl:value-of select="price"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

LAB EXERCISE - 8
Ques. Create a Windows Application Form.
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 db1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
label4.Visible = true;
if (textBox1.Text == "osama" && textBox2.Text == "abcde")
{
label4.Text = "Hello Osama ";
label4.ForeColor = Color.Green;
}
else
{
label4.Text = "Invalid Password";
label4.ForeColor = Color.Red;
}
}
}}

You might also like