Dot Net Lab Report
Dot Net Lab Report
(Tribhuvan University)
Kamaladi Mod, Kathmandu
Submitted By
Name: Riyaz Shrestha
Roll No: 27
Submitted To
Divya Gyan College
Department of Bachelor of Computer Application
Kamaladi Mod, Kathmandu, Nepal.
Table of Contents
Source Code:
using System;
namespace Project1
{
internal class Class1
{
public class Calculation
{
//non parameterized method
public void Add()
{
Console.WriteLine("This is non-parameterized
method");
}
//parameterized method
public void Add(int x, int y)
{
Console.WriteLine("This is parameterized
method.\nSum:"+(x+y));
}
}
static void Main(string[] args)
{
Calculation cal = new Calculation();
cal.Add();
cal.Add(10,20);
Console.ReadLine();
}
}
}
1
Output:
Conclusion:
From this lab, we learned to implement class and methods. Hence, a class
Calculation is created and parameterized and non-parameterized methods are created and
output is displayed.
Source Code:
using System;
namespace Chapter_Two_Arrays
{
internal class Program
{
static void Main(string[] args)
{
//Infinite length Array
string[] cars;
cars = new string[] { "Tiago", "Nexon", "Polo",
"Volkswagen" };
2
schools[4] = "St. Marys";
Console.WriteLine("---Infinite Length Array---");
foreach (string s in cars) {
Console.WriteLine(s);
}
Console.WriteLine("---Fixed Length Array---");
foreach (string s in schools) {
Console.WriteLine(s);
}
Console.ReadLine();
}
}
}
Output:
Conclusion:
From this lab, we learned to implement class and methods. Hence, a class
Calculation is created and parameterized and non-parameterized methods are created and
output is displayed.
3
Lab 3: Implementation of If and Switch Statements
Objective:
- To implement If and Switch statements
using System;
namespace Chapter_Two_If_Statement
{
internal class Program
{
static void Main(string[] args)
{
int number = 15;
Output:
4
Source Code of Switch Statements:
using System;
namespace Chapter_Two_If_Statement
{
internal class Program
{
static void Main(string[] args)
{
int iNo;
case 50:
Console.WriteLine("Value is 50");
break;
case 25:
Console.WriteLine("Value is 25");
break;
default:
Console.WriteLine("Value is "+iNo);
break;
}
Console.ReadLine();
}
}
}
Output:
5
Lab 4: Implementation of For and Foreach Loop
Objective:
using System;
namespace Chapter_Two_For_Each_Loop
{
internal class Program
{
static void Main(string[] args)
{
string[] strMotors = { "Nexon", "BYD", "Thar",
"Hilux" };
Console.WriteLine("Using ForEach Loop");
foreach (string s in strMotors)
Console.WriteLine(s);
Console.ReadLine();
}
}
}
Output:
Conclusion:
From this lab, we learned to implement for and foreach loop. Hence, elements of an
array are displayed using for and foreach loop.
6
Lab 5: Implementation of While and Do-While Loop
Objective:
using System;
namespace Chapter_Two_Do_While_Loop
{
internal class Program
{
static void Main(string[] args)
{
int iNo,iNo1;
Console.WriteLine("Enter a number: ");
iNo=Convert.ToInt32(Console.ReadLine());
iNo1 = iNo;
Console.WriteLine("---Using While Loop---");
while (iNo > 0)
{
Console.WriteLine(iNo);
iNo--;
}
Console.WriteLine("\n---Using Do-While Loop---");
do
{
Console.WriteLine(iNo1);
iNo1--;
}while(iNo1 > 0);
Console.ReadLine();
}
}
}
7
Output:
Conclusion:
From this lab, we learned to implement while and do-while loop. Hence, the
numbers are displayed using while and do-while loop.
- To implement constructors
Source Code:
using System;
namespace Chapter_Three_Constructors
{
internal class Program
{
public static int iRollNo;
public static string myName;
public int MyClass;
static Program()
{
8
iRollNo = 2;
myName = "Hari";
}
public Program()
{
MyClass = 10;
}
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine(iRollNo);
Console.WriteLine("My name is " + myName);
Console.WriteLine("I study in class " + p.MyClass);
Console.ReadLine();
}
}
}
Output:
Conclusion:
From this lab, we learned to implement static and non-static constructors. Hence,
the constructors are created and the values are displayed.
9
Lab 7: Implementation of Inheritance
Objective:
- To implement inheritance
Source Code:
using System;
namespace Chapter_Three_Inheritance
{
internal class Program
{
public class Animal
{
public void Sound(){
Console.WriteLine("Animal makes sound.");
}
}
public class Dog: Animal
{
public void MakeSound(){
Console.WriteLine("Dog Barks.");
}
}
static void Main(string[] args)
{
Dog dog = new Dog();
dog.Sound();
dog.MakeSound();
Console.ReadLine();
}
}
}
Output:
10
Lab 8: Implementation of Abstract Class and Method
Objective:
using System;
namespace Chapter_Three_Abstract_Class
{
internal class Program
{
public abstract class MyClass
{
public abstract void Enroll();
}
public class School : MyClass
{
public override void Enroll()
{
Console.WriteLine("Ram Enrolled in 5th
Semester");
}
}
static void Main(string[] args)
{
Output:
Conclusion:
From this lab, we learned to implement abstract class and method. Hence, an
abstract class and method are created and displayed.
11
Lab 9: Implementation of Method Overloading
Objective:
using System;
namespace Chapter_Three_Abstract_Class
{
internal class Program
{
public class Calculation
{
public void Add(int x, int y)
{
Console.WriteLine("Sum: " + (x + y));
}
public void Add(int x, int y,int z)
{
Console.WriteLine("Sum: " + (x + y + z));
}
}
static void Main(string[] args)
{
Calculation calc = new Calculation();
calc.Add(1, 2);
calc.Add(2, 3, 4);
Console.ReadLine();
}
}
}
Output:
Conclusion:
From this lad, we learned to implement method overloading. Hence, two methods
Add with different parameters are created and the results are displayed.
12
Lab 10: Implementation of Interface
Objective:
- To implement interface
Source Code:
using System;
namespace Chapter_Three_Interface
{
internal class Program
{
interface IAnimal
{
void MakeSound();
}
class Dog : IAnimal
{
public void MakeSound()
{
Console.WriteLine("Dog says: Woof!");
}
}
static void Main(string[] args)
{
IAnimal dog = new Dog();
dog.MakeSound();
Console.ReadLine();
}
}
}
Output:
Conclusion:
From this lab, we learned about interface. Hence, an interface Animal is created and
it is implemented in class Dog and displayed.
13
Lab 11: Implementation of Generics
Objective:
- To implement generics
Source Code:
using System;
namespace Chapter_Three_Generic
{
internal class Program
{
class Box<T>
{
public T Value { get; set; }
}
static void Main(string[] args)
{
Box<int> intBox = new Box<int> { Value = 42 };
Box<string> stringBox = new Box<string> { Value =
"Hello, Generics!" };
Output:
Conclusion:
From this lab, we learned about generics. Hence, generics is created and the values
are set and displayed.
14
Lab 12: Implementation of Delegates
Objective:
- To implement delegates
Source Code:
using System;
namespace Chapter_Four_Delegate
{
internal class Program
{
public delegate void Calculation(int a, int b);
static void Add(int a, int b) => Console.WriteLine("Sum:
" + (a + b));
static void Sub(int a, int b) =>
Console.WriteLine("Difference: " + (a - b));
static void Main(string[] args)
{
Calculation c = Add;
c(10, 2);
c = Sub;
c(10, 5);
Console.ReadLine();
}
}
}
Output:
Conclusion:
From this lab, we learned to implement delegates. Hence, a delegate Calculation is
created and methods are assigned and results are displayed.
15
Lab 13: Implementation of Lambda Expression
Objective:
using System;
using System.Collections.Generic;
namespace Chapter_Four_Lambda
{
internal class Program
{
static void Main(string[] args)
{
List<int> mylist = new List<int> {
1,2,3,4,5,6,7,8,9,10,11,12,13};
Console.WriteLine("\nEven Numbers Using Lambda:");
List<int>listeven=mylist.FindAll(x=>x%2==0);
for(int i = 0;i<listeven.Count;i++)
Console.WriteLine(listeven[i]);
Console.ReadLine();
}
}
}
Output:
Conclusion:
From this lab, we learned to implement lambda expression. Hence, a list is created
with numbers and even numbers are displayed using lambda expression.
16
Lab 14: Implementation of Exception Handling
Objective:
using System;
namespace CHapter_Four_Try_Catch
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Number");
try
{
int num = Convert.ToInt32(Console.ReadLine());
int iNo = num / 2;
}
catch (Exception ex) {
if (ex.GetType().ToString() ==
"System.FormatException")
{
Console.WriteLine("Enter a numeric value.");
}
}
Console.ReadLine();
}
}
}
Output:
17
Lab 15: Implementation of LINQ
Objective:
- To implement LINQ
Source Code:
using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQ
{
public class Student
{
public string Name { get; set; }
public string Address { get; set; }
}
internal class Program
{
public static void Main(string[] args)
{
List<Student> students = new List<Student>
{
new Student{Name="Ram",Address="Kirtipur"},
new Student{Name="Hari",Address="Kathmandu"},
new Student{Name="Sita",Address="Kirtipur"},
};
var res = students.Where(s => s.Address ==
"Kirtipur").ToList();
foreach(var student in res)
{
Console.WriteLine($"Name:{student.Name},Address:{
student.Address}");
}
Console.ReadLine();
}
}
}
Output:
18
Lab 16: Implementation of Web Forms
Objective:
</asp:Content>
PageCode.aspx.cs:
using System;
namespace Basic_Application_Web_Forms
{
public partial class PageCode : System.Web.UI.Page
{
19
protected void Button1_Click(object sender, EventArgs e)
{
float f = Convert.ToInt32(TextBox1.Text);
float g = Convert.ToInt32(TextBox2.Text);
float h = f + g;
Label1.Text = h.ToString();
Label1.Visible = true;
}
}
}
Output:
Conclusion:
From this lab, we learned to implement asp.NET web forms. Hence a webpage is
created using web forms that takes two numbers and displays the result.
20
Lab 17: Implementation of Database
Objective:
- To implement database
Source Code:
Page.aspx:
21
<asp:Label ID="Label1" runat="server" Text="Label"
Visible="false"></asp:Label>
</asp:Content>
Page.aspx.cs:
using System;
using System.Configuration;
using System.Data.SqlClient;
namespace Basic_Application_Web_Forms
{
public partial class ListPage : System.Web.UI.Page
{
SqlConnection _conn;
SqlCommand _command;
string strQuery, strCon;
protected void Button1_Click(object sender, EventArgs e)
{
strCon =
ConfigurationManager.ConnectionStrings["ConnectDatabase"].Connect
ionString;
if (firstname.Text != "" && lastname.Text != "" &&
address.Text != "" && phone.Text != "" && email.Text != "")
{
_conn = new SqlConnection(strCon);
_conn.Open();
strQuery = "INSERT INTO Students (FirstName,
LastName, Address, Phone, Email) " +
"VALUES ('" + firstname.Text + "', '" + lastname.Text + "',
'" + address.Text + "', '" + phone.Text + "', '" + email.Text +
"')";
_command = new SqlCommand(strQuery, _conn);
22
}
}
Output (Database):
Conclusion:
From this lab, we learned about database connectivity in asp.NET. Hence, a
webpage is created using .NET web forms and a database STUDENT is connected and data
is inserted and stored in the table and displayed.
23