Write A Program in C# To Demonstrate Command Line Arguments Processing
Write A Program in C# To Demonstrate Command Line Arguments Processing
Assignment 1
/**********************************************************************************/
using System;
namespace CSharpProgram1
class Program
Console.WriteLine (obj);
Output:
Assignment 2
object
dynamic
string
Call by Value means calling a method with a parameter as value. Through this, the argument
value is passed to the parameter.
While Call by Reference means calling a method with a parameter as a reference. Through this,
the argument reference is passed to the parameter.
In call by value, the modification done to the parameter passed does not reflect in the caller's
scope while in the call by reference, the modification done to the parameter passed are persistent
and changes are reflected in the caller's scope.
Boxing
Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit
conversion of a value type to the type object or to any interface type implemented by this value
type. Boxing a value type allocates an object instance on the heap and copies the value into the
new object.
Un boxing
Un boxing is an explicit conversion from the type object to a value type or from an interface type
to a value type that implements the interface. An unboxing operation consists of:
Checking the object instance to make sure that it is a boxed value of the given value type.
Copying the value from the instance into the value-type variable.
/********* *******************************************************************/
using System;
namespace CallByValue {
class Program
Output:
using System;
namespace CallByReference {
class Program
int val = 5;
Output:
class Test_Boxing
object obj = i;
Output:
class Test_Unboxing
int i = 15;
try {
System.Console.WriteLine("Unboxing OK.");
catch (System.InvalidCastException e)
Output:
Assignment 3
using System;
public Employee()
this.id = id;
this.name = name;
this.salary = salary;
class TestEmployee{
e2.display();
Output:
Assignment 4
Private Modifier
If you declare a field with a private access modifier, it can only be accessed within the same
class:
Public Modifier
If you declare a field with a public access modifier, it is accessible for all classes
namespace accessspecifier
class Program
B.show();
class one
private int x;
protected int y;
internal int z;
public int a;
// x = 10;
y = 20;
z = 30;
a = 40;
b = 50;
Console.WriteLine(y);
Console.WriteLine(z);
Console.WriteLine(a);
Console.WriteLine(b);
Console.ReadLine();
Output:
Assignment 5
Creating a static class is therefore basically the same as creating a class that contains only
static members and a private constructor. A private constructor prevents the class from being
instantiated. The advantage of using a static class is that the compiler can check to make sure that
no instance members are accidentally added. The compiler will guarantee that instances of this
class cannot be created.
Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class
except Object. Static classes cannot contain an instance constructor. However, they can contain a
static constructor. Non-static classes should also define a static constructor if the class contains
static members that require non-trivial initialization. For more information, see Static
Constructors.
/*************************************************************************/
using System;
namespace Static_var_and_fun
Dept of MCA, BEC BGK 2020-21 Page 13
.NET Technology (PCA552L) 2BA18MCA05
class number
Console.ReadLine();
class Program
Console.WriteLine("Enter a number:");
number.num = Convert.ToInt32(Console.ReadLine());
number.power();
Output:
Assignment 6
Jagged Array
A jagged array is an array whose elements are arrays. The elements of a jagged array can
be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays."
The following examples show how to declare, initialize, and access jagged arrays.
The following is a declaration of a single-dimensional array that has three elements, each
of which is a single-dimensional array of integers:
A jagged array is an array of arrays, and therefore its elements are reference types and are
initialized to null.
using System;
class Jagged_ArrayTest
{
static void Main()
{
}
System.Console.WriteLine();
}
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
Output:
Assignment 7
Exceptions provide a way to transfer control from one part of a program to another. C#
exception handling is built upon four keywords: try, catch, finally, and throw.
try − A try block identifies a block of code for which particular exceptions is activated.
It is followed by one or more catch blocks.
finally − The finally block is used to execute a given set of statements, whether an
exception is thrown or not thrown. For example, if you open a file, it must be closed
whether an exception is raised or not.
throw − A program throws an exception when a problem shows up. This is done using a
throw keyword.
Handling Exceptions
C# provides a structured solution to the exception handling in the form of try and catch
blocks. Using these blocks the core program statements are separated from the error-handling
statements.
These error handling blocks are implemented using the try, catch, and finally keywords.
Following is an example of throwing an exception when dividing by zero condition occurs .
/***************************************************************************/
using System;
namespace ErrorHandlingApplication
class DivNumbers
int result;
DivNumbers() {
result = 0;
try {
catch (DivideByZeroException e)
finally {
Console.ReadKey();
Output:
Assignment 8
Virtual keyword is used for generating a virtual path for its derived classes on implementing
method overriding.
Override keyword is used in the derived class of the base class in order to override the
base class method.
/* Write a Program to Demonstrate Use of Virtual and override key words in C# with a
simple program.*/
/******************************************************************************/
using System;
namespace VirtualKey
{
class person
{
protected string fname;
protected string lname;
public person(string fn, string ln)
{
fname = fn;
lname = ln;
}
public virtual void display()
{
Console.WriteLine("Person :" + fname + " " + lname);
}
}
class emp:person
{
{
Console.WriteLine ("Employee :" + fname + " " + lname + " " + year);
}
}
class worker : person
{
{
Console.WriteLine("Worker :" + fname + " " + lname + " " + company);
}
}
class Program
{
static void Main(string[]args)
{
Console.WriteLine("\n\n*** VIRTUAL AND OVERRIDE KEYWORDS DEMO ***");
Console.ReadLine ();
}
}
}
Output:
Assignment 9
Delegates are especially used for implementing events and the call-back methods. All
delegates are implicitly derived from the System Delegate class.
Declaring Delegates
Delegate declaration determines the methods that can be referenced by the delegate. A
delegate can refer to a method, which has the same signature as that of the delegate.
Instantiating Delegates
Multicasting of a Delegate
Delegate objects can be composed using the "+" operator. A composed delegate calls the
two delegates it was composed from. Only delegates of the same type can be composed. The "-"
operator can be used to remove a component delegate from a composed delegate.
Using this property of delegates you can create an invocation list of methods that will be
called when a delegate is invoked. This is called multicasting of a delegate.
/* Write a Program in C# to create and implement a delegate for any two arithmetic
operations. */
/************************************************************************/
using System;
namespace delegate_demo
return x + y;
return x - y;
class program {
Console.ReadLine();
Output:
Assignment 10
Abstract class
If a class is defined as abstract then we can't create an instance of that class. By the
creation of the derived class object where an abstract class is inherit from, we can call the
method of the abstract class.
Abstract method
using System;
namespace abstract_demo {
fname = fn;
lname = ln;
year = yr;
company = c;
class Program
p2.display();
p3.display();
Console.ReadLine();
Output:
Assignment 11
Properties are named members of classes, structures, and interfaces. Member variables or
methods in a class or structures are called Fields. Properties are an extension of fields and are
accessed using the same syntax. They use accessors through which the values of the private
fields can be read, written or manipulated.
Properties do not name the storage locations. Instead, they have accessors that read,
write, or compute their values.
For example, let us have a class named Student, with private fields for age, name, and
code. We cannot directly access these fields from outside the class scope, but we can have
properties for accessing these private fields.
Accessors
The accessor of a property contains the executable statements that helps in getting
(reading or computing) or setting (writing) the property. The accessor declarations can contain a
get accessor, a set accessor, or both.
/* Write a program to Set & Get the Name & Age of a person using Properties of C# to
illustrate the use of different properties in C#. */
/************************************************************************/
using System;
namespace properties_demo {
class Student
{
private string code = "N.A";
private string name = "not known";
private int age = 0;
// Declare a Code and name property of type string:
get {
return code;
}
set {
code = value;
}
}
public string Name {
get {
return name;
}
set {
name = value;
}
}
// Declare a Age property of type int:
}
}
public override string ToString() {
return "Code = " + Code +", Name = " + Name + ", Age = " + Age;
}
}
class ExampleDemo {
public static void Main() {
s.Age = 26;
Console.WriteLine("Student Info: {0}", s);
// increasing age
s.Age += 1;
Console.WriteLine("Student Info: {0}", s);
Console.ReadKey();
}
}
}
Output:
Assignment 12
Syntax of Interface
Runtime Polymorphism
We can implement more than one interface in one class. This is the way to achieve the
multiple inheritances. Create two interfaces and create same method in both and implement in
same class.
As you can see that we implemented two interfaces into one class. We give one definition
to the method. We will not get any error as in interface we only declared the method. That is
only signature. So we can give one common definition to all same method having same
signature. When we create the object of implementation class we can easily call sum method
without any error.
/****************************************************************************/
using System;
namespace IntrDemo {
void area();
double pi = 3.142;
float r = float.Parse(Console.ReadLine());
class program
s[i].area();
Output:
Assignment 13
/*****************************************************************/
Design page view
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;
using System.Data.SqlClient;
using System.Data.Common;
namespace Student
public Form1()
InitializeComponent();
String username;
username = uname.Text;
String pass;
pass = password.Text;
String query = "insert into student_info values ('" + username + "','" + pass + "')";
con.Open();
cmd.ExecuteNonQuery();
con.Close();
String username;
username = uname.Text;
String pass;
pass = password.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
String username;
username = uname.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
con.Open();
da.Fill(ds);
cmd.ExecuteNonQuery();
dataGridView1.DataSource = ds.Tables[0];
con.Close();
Assignment 14
Design and implement ASP.NET page to demonstrate basic validation specification.
/****************************************************************************/
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
height: 31px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="2" >
<tr><td colspan="3" align="center"> Student Registration </td></tr>
<tr>
<td><asp:Label ID="user_name" Text="Student Name" runat="server"></asp:Label></td>
<td><asp:TextBox ID="uname" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="rdvalid1" runat="server" ControlToValidate="uname"
ErrorMessage="Enter your name">
</asp:RequiredFieldValidator>
<br />
</td>
</tr>
<tr>
<td><asp:Label ID="Label5" Text="Gender" runat="server">
</asp:Label></td>
<td>
<asp:RadioButtonList ID="gender" runat="server" RepeatLayout="Flow">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="gender" ErrorMessage="Select your gender">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td><asp:Label ID="semlb" Text="Semester" runat="server"></asp:Label></td>
<td><asp:TextBox ID="sem" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="sem" ErrorMessage="Enter your semester">
</asp:RequiredFieldValidator>
<asp:RangeValidator ID="semrng" runat="server" MinimumValue="1"
MaximumValue="6" Type="Integer" ControlToValidate="sem" ErrorMessage="Enter your
sem(1-6)"></asp:RangeValidator></td>
</tr>
<tr>
<td><asp:Label ID="Label6" Text="Phone Number" runat="server"></asp:Label></td>
<td><asp:TextBox ID="phone" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="phone" ErrorMessage="Enter your phone number">
</asp:RequiredFieldValidator>
<tr>
<td class="style3">
Email:
</td>
<td class="style2">
<asp:TextBox ID="stdemail" runat="server" style="width:250px">
</asp:TextBox>
</td>
<td>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server"
ControlToValidate="stdemail" ErrorMessage="Enter your valid email id"
ValidationExpression="\w+([-+.']\w+)@\w+([-.]\w+)\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="style3" align="center" colspan="3"></td>
</tr>
<tr>
<td class="style3" align="center" colspan="3">
<asp:Button ID="btnsubmit" runat="server" Text="Submit" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Output:
Assignment 15
Design and implement web application to validate user login.
/***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Text;
using System.Data;
using System.ComponentModel;
using System.Drawing;
}
SqlConnection con = new SqlConnection("Data Source=DESKTOP-
01APTPN\\SQLEXPRESS;Initial Catalog=Logindatabase;Integrated Security=True");
int count;
DataTable datatable;
DataSet ds = new DataSet();
if (uname.Text == "")
{
Response.Write("Enter Username");
}
con.Open();
String Sql = "select * from login where (User_Name='" + uname.Text + "' and Password='"
+ pass.Text + "')";
SqlCommand cmd = new SqlCommand(Sql, con);
count = cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
datatable = ds.Tables[0];
count = datatable.Rows.Count;
if (count > 0)
{
Response.Write("Login Successful");
}
else
{
Response.Write("Login UnSuccessful");
}
con.Close();
}
Output: