net[1] (1)
net[1] (1)
net[1] (1)
Theory
• Properties are used to access or set the value of a private field from outside of the class. A property
has a get accessor, which retrieves the value of the private field, and a set accessor, which sets the
value of the private field.
• Properties make code more readable and maintainable by providing a consistent way to access and
modify data. They allow for easy modification of the underlying data
Procedure
using System;
class Person
{ private string name; //
field
public string Name // property
{
get { return name; }
set { name = value; }
}
}
class Program
{
static void Main(string[] args)
{
Person myObj = new Person();
myObj.Name = "Aman";
Console.WriteLine(myObj.Name);
Output:
Aman
Experiment:-02
Theory
In C#, strings are implemented as objects of the built-in string class. The string class is a reference
type and stores strings as an array of characters.
This can be done using loops and array indexing, along with built-in C# functions such as the
String.Concat(), String.Substring(), and String.Replace() methods.
Procedure
using System;
using
System.Collections.Generic;
using System.Linq; using
System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
char[] ch = { 'A', 'B', 'C', 'D', 'E' };
char[] ch1 = new
char[5]; int i = 0;
ch1[0] = 'a'; ch1[1] =
'b'; ch1[2] = 'c';
ch1[3] = 'd';
ch1[4] = 'e';
Console.WriteLine("TheString is: ");
for (i = 0; i < ch.Length; i++)
{
Console.Write(ch[i] + "");
}
Console.WriteLine();
}
}
}
Output:
First array:ABCDE
Experiment:-03
Theory
An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum
of its own digits each raised to the power of the number of digits. For example, 153 is an
Armstrong number because 1^3 + 5^3 + 3^3 = 153.
Procedure
using System;
public class Armstrong
{
public static void
Main(){ int
num,r,sum,temp; int
stno,enno;
Console.Write("Input starting number of range: "); stno=
Convert.ToInt32(Console.ReadLine());
Console.Write("Input ending number of range : "); enno=
Convert.ToInt32(Console.ReadLine());
Console.Write("Armstrong numbers in given range are: ");
for(num=stno;num<=enno;num++){
temp=num; sum = 0;
while(temp!=0){ r=temp %
10; temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
Console.Write("{0} ",num);
}
Console.Write("\n");
}
Output:
Input starting number of range: 1
Input ending number of range : 500
Armstrong numbers in given range are: 1 153 370 371 407
Experiment :-4(a)
Aim:-Create a console application to calculate area of circle. Accept radius from user Calculate
circle area and print it.
Theory
To create a console application in Visual Studio that calculates the area of a circle, you can follow
these steps:
1. Open Visual Studio and create a new project by selecting "Console App (.NET Core)" from
the list of project templates.
2. In the Main method, use the Console.ReadLine() method to accept the radius of the circle
from the user. Store the radius in a variable.
3. Use the following formula to calculate the area of the circle: area = pi * radius * radius where
pi is a constant with a value of approximately 3.14.
4. Use the Console.WriteLine() method to print the calculated area to the console.
5. In the program.cs file add the following code snippet given in suggested procedure
6. Press F5 to run the application and test it by entering different radius value.
Procedure
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the radius of the circle:");
double radius = double.Parse(Console.ReadLine()); double
area = Math.PI * (radius * radius);
Console.WriteLine("The area of the circle is: " + area);
Console.ReadLine();
}
}
}
Output:
Enter the radius of the circle: 5
The area of the circle is: 78.53981633974483
Experiment :-4(b)
Aim:- Create a console application to build a simple calculator. Calculator will have following
functions Accept 2 numbers Perform Add/Sub/Div/Mult Print Result.
Theory
Creating a console application to build a simple calculator in Visual Studio using C# involves
several steps:
1. Open Visual Studio and create a new project by selecting "Console App (.NET Core)" from
the list of project templates.
2. In the Main method, use the Console.ReadLine() method to accept the two numbers from the
user. Store the numbers in two different variables.
3. Use mathematical operators such as +, -, * and / to perform the addition, subtraction,
multiplication and division respectively.
4. Use the Console.WriteLine() method to print the result of the performed operation to the
console.
5. In the program.cs file add the following code snippet given in suggested procedure 6. Press F5
to run the application and test it by entering different numbers and operations.
Procedure
using System;
public class
Exercise
{
public static void Main()
{
int num1,num2,opt;
switch(opt)
{ case 1:
Console.Write("The Addition of {0} and {1} is: {2}\n",num1,num2,num1+num2);
break;
case 2:
Console.Write("The Substraction of {0} and {1} is: {2}\n",num1,num2,num1-num2);
break;
case 3:
Console.Write("The Multiplication of {0} and {1} is: {2}\n",num1,num2,num1*num2);
break;
case 4:
if(num2==0) {
Console.Write("The second integer is zero. Devide by zero.\n");
} else {
Console.Write("The Division of {0} and {1} is : {2}\n",num1,num2,num1/num2);
}
break;
case 5:
break;
default:
Console.Write("Input correct option\n");
break;
}
}
}
Output:
Enter the first Integer :20
Enter the second Integer :4
Here are the options :
1-Addition.
2-Substraction.
3-Multiplication.
4-Division.
5-Exit.
Input your choice :4
The Division of 20 and 4 is : 5
Experiment:-05
Theory
In C#, an exception is an event that occurs during the execution of a program that disrupts the
normal flow of instructions. When an exception occurs, the program looks for an exception
handler, which is a block of code that can handle the exception. If an exception handler is not
found, the program will terminate.
C# provides several predefined exception classes that are defined in the System namespace. These
classes are derived from the base class Exception, and they represent specific types of exception.
Procedure
class Program : System.Exception {
static void Main(string[] args)
{
int[] arr = { 1, 2, 3, 4, 5 };
try {
Console.WriteLine(arr[7]);
}
catch (IndexOutOfRangeException e) {
Theory
The theory behind writing a program in C# to implement the concept of abstract and sealed classes
is rooted in object-oriented programming (OOP) principles.
1. Abstract classes: An abstract class is a class that cannot be instantiated. It is used as a base
class for other classes that can be instantiated.
2. Encapsulation: Encapsulation is the mechanism of hiding the internal state and behavior of an
object, and exposing only a public interface.
3. Inheritance: Inheritance is the mechanism of creating a new class based on an existing class.
4. Polymorphism: Polymorphism is the ability of an object to take on many forms.
Procedure
Abstract Class :-
using System;
namespace AbstractClass
{ abstract class Animal {
public abstract void makeSound();
}
}
Sealed Class :-
class Program {
static void Main(string[] args)
{
SealedClass slc = new SealedClass();
int total = slc.Add(6, 4);
Console.WriteLine("Total = " + total.ToString());
}
}
Bark Bark
Theory
The relevant theory behind writing a program in C# to implement ADO.Net database connectivity
involves understanding the following concepts:
1. ADO.Net architecture: ADO.Net is a data access technology for accessing data from databases.
It provides a set of classes and interfaces that allow developers to interact with data stored in
databases.
2. Connection objects: Connection objects are used to establish a connection between the .NET
application and the database. They are responsible for opening and closing the connection to
the database.
3. Command objects: Command objects are used to execute SQL commands on a database. They
can be used to execute both data retrieval and data modification commands.
4. DataReader objects: DataReader objects are used to retrieve data from a database in a
forward-only, read-only manner. They are optimized for performance and are ideal for
situations where you need to retrieve a large amount of data.
5. DataSet objects: DataSet objects are in-memory representations of data retrieved from a
database. They provide a flexible, disconnected data model that allows developers to work
with data in a disconnected manner.
6. DataAdapter objects: DataAdapter objects are used to fill DataSet objects with data from a
database. They also provide the ability to update a database with changes made to the data in
the DataSet.
7. Transaction management: ADO.Net provides the ability to manage transactions, allowing
developers to ensure the integrity of their data by committing or rolling back a set of changes
as a single unit.
Procedure
using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication
{
class Program
{
static void Main(string[] args)
{
try
{
string ConString = "data source=.; database=StudentDB; integrated security=SSPI";
using (SqlConnection connection = new SqlConnection(ConString))
{
// Creating SqlCommand objcet
SqlCommand cm = new SqlCommand("select * from student", connection);
// Opening Connection
connection.Open();
}
}
Output:
Experiment:-08
Theory
The theory behind writing a program in C# to implement the concept of data streams the
understanding of input and output operations. There are two types of data streams in C#: input
streams and output streams.
In C#, data streams are implemented using classes in the System.IO namespace. Some of the
commonly used classes in this namespace include:
• FileStream
• MemoryStream
• NetworkStream
• BufferedStream
Procedure
using System; using
System.IO; using
System.Threading.Tasks;
namespace DemoApplication
{
class Tutorial
{
static void Main(string[] args)
{
String path = @"D:\Example.txt";
using (StreamReader sr = File.OpenText(path))
{
String s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
Console.ReadKey();
}
}
Output:
THIS IS A DEMO FILE
Experiment:-09
Theory
The theory behind events and delegates in C# is centered around the concepts of event-driven
programming and delegate objects.
In event-driven programming, the flow of a program is determined by events. These events are
raised or triggered by user interactions, system events or other programmatic operations. To
handle these events, delegate objects are used to create a link between the event and the event
handler method.
Procedure
using System;
namespace SampleApp {
public delegate string MyDel(string str);
class EventProgram {
event MyDel MyEvent;
public EventProgram() {
this.MyEvent += new MyDel(this.WelcomeUser);
}
public string WelcomeUser(string username)
{ return "Welcome " + username;
}
static void Main(string[] args) {
EventProgram obj1 = new EventProgram();
string result = obj1.MyEvent("CSE 2024-2025");
Console.WriteLine(result);
}
Output:
Welcome CSE 2024-2025
Experiment:-10
Theory
The relevant theory behind designing a web-based database connectivity form using ASP.NET
involves understanding the following concepts:
1. HTML and CSS: Understanding the basics of HTML and CSS will help in designing the
user interface of the web form.
2. ASP.NET: ASP.NET is a web development platform that provides a framework for
building dynamic web applications.
3. ADO.NET: ADO.NET is the data access technology used in ASP.NET to interact with
databases.
4. Web Forms: Web Forms are the primary building blocks for creating ASP.NET web
applications.
5. Data Binding: Data binding is the process of connecting a control on the web form to a
data source, such as a database, to display data.
6. Connection Strings: Connection strings are used to specify the connection details of a
database. This includes the database server, database name, username, and password.
7. SQL Server: SQL Server is a relational database management system used in ASP.NET to
store data.
These are some of the key concepts that are relevant to designing a web-based database
connectivity form using ASP.NET.
Procedure
If you want to connect to the SQL database into ASP.NET, using C#, it should follow the steps
given below.
Step 1
Now, Open Visual Studio 2015 Update 3, go to the File >> New >> Project or use the shortcut key
"Ctrl+Shift +N".
Step 2
Here, select Visual C# >> Web >> ASP.NET Web Application. Finally, click "OK" button.
Step 3
Here, you can select the template for your ASP.NET Application. We are choosing "Empty" here.
Now, click OK button.
Step 4
Now, open the project and look for the Solution Explorer.
Here, open the default .aspx. If you want a Webform, you can add the page (Web Form).
Add+New item (Ctrl+Shift+A).
Now, we can create a login page, using ASP.NET code. You need to follow the drag and drop
method. Here, we created the login page.
Step 5
Now, open the project. If you want a SQL Server database, you can add the page (SQL Server
database). Add+New item (Ctrl+Shift+A).
Here, you can select Visual C# and choose SQL Server database. Afterwards, click "OK" button.
Step 6
Now, we can go to the Server Explorer and add your database. You can click the Tables and
afterwards, click Add New Table.
Now, open the new table and you can fill the data, which is like (studentname, password) and
afterwards, you click the Update .
Here, click database in update and subsequently click update the database.
Step 7
Now, you can add SQL Data Source. Drag and drop method. Here, click Configure Data Source
Now, we can choose your database and click NEXT button.
Now, you can select the ConnectionString and Click NEXT button
Now, we can choose Specify columns from a table or view and afterwards, click Next button.
Step 8
Now, you can go to CS(C# code) page and you will write the C# code.
using System;
using System.Linq; using
System.Web; using
System.Web.UI; using
namespace DatabaseConnectivity
{
public partial class loginpage System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionString
s["RegiConnectionString"].ConnectionString);
conn.Open();
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Bold="True"
ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
</aspGridView>
</div>
<div id="last">
</div>
</form>
</body>
</html>
Step 9
Here, you need to run any Browser and after a few minutes, you will get some output. Now, we
can insert the data into your database.
Step 10
Now, we can added the GridView. Drag and drop method needs to be used.
Now, you can choose the data source, it is sqlDataSource.
Step 11
Here, you need to run any Browser and after a few minutes, you will get an output. Now, we can
view the data.
Output:
Experiment:-11
Theory
The relevant theory behind writing a program in C# to implement indexers is object-oriented
programming (OOP) concepts, specifically the use of classes. Indexers allow a class to be
accessed like an array, allowing elements within the class to be accessed through index values.
This provides a convenient way to access elements within a class, especially when the underlying
data is stored in arrays or other collections. Additionally, indexers can be overloaded, meaning
that different types of index values can be used to access different elements within the class.
Procedure
using System; namespace
IndexerApplication { class
IndexedNames {
private string[] namelist = new string[size];
static public int size = 6;
public IndexedNames() {
for (int i = 0; i < size; i++)
namelist[i] = "N. A.";
}
public string this[int index] {
get
{ string
tmp;
if( index >= 0 && index <= size-1 )
{ tmp = namelist[index];
} else
{ tmp
= ""; }
return ( tmp );
}
set {
if( index >= 0 && index <= size-1 )
{ namelist[index] = value;
}
}
}
static void Main(string[] args) {
IndexedNames names = new IndexedNames();
names[0] = "Zara"; names[1] = "Riz";
names[2] = "Nuha"; names[3] = "Davinder";
names[4] = "Sunil"; for ( int i = 0; i <
IndexedNames.size; i++ ) {
Console.WriteLine(names[i]);
}
Console.ReadKey();
}
}
}
Output:
Zara
Riz
Nuha
Davinder
Sun
il N.
A.