1) Write A Program in C# To Demonstrate Command Line Arguments Processing. Command Line Arguments
1) Write A Program in C# To Demonstrate Command Line Arguments Processing. Command Line Arguments
1) Write A Program in C# To Demonstrate Command Line Arguments Processing. Command Line Arguments
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace commandline
{
class Program
{
static void Main(string[] args)
{
int SUM = 0;
int X = 0;
int Y = 0;
X = Convert.ToInt32(args[0]);
Y = Convert.ToInt32(args[1]);
SUM = X + Y;
}
}
}
OUTPUT:-
Reference Type
It refers to a memory location. Using multiple variables, the reference types can refer to a
memory location. If the data in the memory location is changed by one of the variables,
the other variable automatically reflects this change in value.
object
dynamic
string
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.
Unboxing
Unboxing 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.
OUTPUT:-
namespace unboxing
{
class Program
{
static void Main(string[] args)
{
}
}
}
OUTPUT:-
When you use this keyword to call a constructor, the constructor should
belong to the same class.
You can also pass parameter in this keyword.
This keyword always pointing to the members of the same class in which it is
used.
When you use this keyword, it tells the compiler to invoke the default
constructor. Or in other words, it means a constructor that does not contain arguments.
This keyword contains the same type and the same number of parameters that
are present in the calling constructor.
This concept removes the assignment of replication of properties in the same class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace thiskeyword
{
class Program
{
static void Main(string[] args)
{
Amount amt = new Amount();
amt.Balance();
amt.GetData();
Console.WriteLine("\n Press Enter to Quit....");
Console.ReadLine();
double totalamount,deduction,Balanceamount;
Console.WriteLine("-------------------------");
Console.WriteLine("Press (d) for the Debit and (c) for Credit:");
Data=char.Parse(Console.ReadLine());
if(Data=='d'){
Debit();
}
if(Data=='c'){
Credit();
}
} // end of GetData()
OUTPUT:-
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:
//private modifiers.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab4
{
class student
{
private string Name="Abhishek";
}
class Program
{
static void Main(string[]args)
{
student myObj = new student();
Console.WriteLine(myObj.Name);
Console.ReadLine();
}} }
OUTPUT:-
//PublicModifier.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab4
{
class student
{
public string Name="Abhishek";
}
class Program
{
static void Main(string[]args)
{
student myObj = new student();
Console.WriteLine(myObj.Name);
Console.ReadLine();
}
}
}
OUTPUT:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace staticmethod
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please Select the convertor");
Console.WriteLine("1. Celsius To Fahrenheit");
Console.WriteLine("2. Fahrenheit To Celsius");
Console.Write("\n Press either 1 or 2 : ");
string choice;
choice = Console.ReadLine();
double F, C = 0;
switch (choice)
{
case "1":
Console.WriteLine(" Enter Temparature in Celsius");
F = temparature_converter.CelsiusToFahrenheit(Console.ReadLine());
Console.WriteLine("Temparature in Fahrenheit: {0}", F);
break;
case "2":
Console.WriteLine(" Enter Temparature in Fahrenheit");
C = temparature_converter.FahrenheitToCelsiu(Console.ReadLine());
Console.WriteLine("Temparature in Celsiu: {0}", C);
break;
OUTPUT:-
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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace jaggedArray
{
class Program
{
static void Main(string[] args)
{
// Declare the array of four elements:
int[][] jaggedArray = new int[4][];
// Initialize the elements:
jaggedArray[0] = new int[2] { 7, 9 };
jaggedArray[1] = new int[4] { 12, 42, 26, 38 };
jaggedArray[2] = new int[6] { 3, 5, 7, 9, 11, 13 };
Dept. of MCA, BECBGK 2020-21 Page 17
.Net Technologies Laboratory (PCA552L) 2BA18MCA01
OUTPUT:-
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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace exceptionhandling
{
class Program
{
static void Main()
{
int number = 0;
int div = 0;
Console.WriteLine("Enter the number");
number = int.Parse(Console.ReadLine());
try
{
div = 150 / number;
Console.WriteLine(" You are in try block");
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Exception occurs: " + ex.Message);
}
finally
{
Console.WriteLine(" You are in finally block");
Console.WriteLine(" Div=" + div);
}
Console.ReadLine();
}
}
}
OUTPUT:-
{
Console.WriteLine(" You are in finally block");
Console.WriteLine(" Div=" + div);
}
Console.ReadLine();
}
}
}
OUTPUT:-
Virtual keyword is used for generating a virtual path for its derived classes on implementing
method overriding.
o Virtual keyword is used within a set with override keyword.
Override keyword is used in the derived class of the base class in order to override the
base class method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Virtualandoverride
{
public class Program
{
public virtual void getdata()
{
Console.WriteLine("Enter the name of the student:");
string name = Console.ReadLine();
Console.WriteLine("Enter the USN:");
string usn = Console.ReadLine();
}
}
class student:Program
{
public override void getdata()
{
base.getdata();
Console .WriteLine ("Enter the marks for differnt Subjects");
Console .WriteLine ("C#:");
int csharp=int.Parse (Console .ReadLine());
Console .WriteLine ("\n CPP:");
int cpp=int.Parse (Console .ReadLine ());
Console .WriteLine ("\n C:");
int c=int.Parse (Console .ReadLine ());
Console .WriteLine ("\n Total={0}",csharp +cpp +c );
}
static void Main()
{
// LABPGM6 obj = new LABPGM6 ();
//obj.getdata();
student obj1 = new student();
obj1.getdata();
Console.ReadLine();
}
}
}
OUTPUT:-
A delegate is a type-safe object that can point to another method (or possibly multiple
methods) in the application, which can be invoked at later time.
Defining a Delegate.
When you want to create a delegate in C# you make use of delegate keyword.The name of your
delegate can be whatever you desire. However, you must define the delegate to match the
signature of the method it will point to.
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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Delegate
{
// Delegate Definition
public delegate int operation(int x, int y);
class Program
{
// Method that is passes as an Argument
// It has same signature as Delegates
static int Add(int a, int b)
{
return a + b;
}
static int sub(int a, int b)
{
return a - b;
}
static int mul(int a, int b)
{
return a * b;
}
static int div(int a, int b)
{
return a / b;
}
static void Main(string[] args)
{
// Delegate instantiation
operation obj = new operation(Program.Add);
operation obj1 = new operation(Program.mul);
operation obj2 = new operation(Program.sub);
operation obj3 = new operation(Program.div);
// output
Console.WriteLine("Addition is={0}",obj(23,27));
Console.WriteLine("Subtraction is={0}", obj2(10, 30));
Console.WriteLine("multiplication is={0}", obj1(10, 30));
Console.WriteLine("Division is={0}", obj3(10,2));
Console.ReadLine();
}
}
}
OUTPUT:-
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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace abstractclass
{
abstract class vehicle
{
protected float price;
protected string model;
public abstract double CalculateEMI(int months, double rate);
}
class car : vehicle
{
public car(int price, string model)
{
this.price = price;
this.model = model;
}
public override double CalculateEMI(int months, double rate)
{
double total = price + price * rate / 100;
double totalEMI = total / months;
return totalEMI;
}
public void display()
{
Console.WriteLine("Car Model={0}", model);
Console.WriteLine("Car Price={0}", price);
}
}
class Pragram
{
public static void Main()
{
car c = new car(500000, "Indica Vz");
c.display();
Console.WriteLine("Enter the no.of months and interest rate");
int mnt = int.Parse(Console.ReadLine());
float rate = float.Parse(Console.ReadLine());
OUTPUT:-
11) 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#.
Properties in C# and .NET have various access levels that is defined by an access
modifier. Properties can be read-write, read-only, or write-only. The read-write property
implements both, a get and a set accessor. A write-only property implements a set accessor, but
no get accessor. A read-only property implements a get accessor, but no set accessor.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace getandset
{
class Student
{
private string myName = "Raju";
private int myAge = 0;
public string Name
{
get
{
return myName;
}
set
{
myName = value;
}
}
public int Age
{
get
{
return myAge;
}
set
{
myAge = value;
}
}
OUTPUT:-
Interface in C# is basically a contract in which we declare only signature. The class
which implemented this interface will define these signatures. Interface is also a way to
achieve runtime polymorphism. We can add method, event, properties and indexers in interface.
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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace polymorphism
{
public interface shape
{
void area();
}
public class Circle : shape
{
public void area()
{
Console.WriteLine("Caluclatig area of Circle");
Console.WriteLine("-------------------------");
Console.WriteLine("Enter the radius of Circle:");
double radius = double.Parse(Console.ReadLine());
double area = 3.14 * radius * radius;
Console.WriteLine("Area of Circle is:{0}", area);
}
}
public class Square : shape
{
public void area()
{
Console.WriteLine("\n\nCaluclatig area of Square");
Console.WriteLine("-----------------------------");
Console.WriteLine("Enter the side:");
float side = float.Parse(Console.ReadLine());
Console.WriteLine("Area of Square is:{0}", side * side);
}
}
class program
{
OUTPUT:-
{
}
private void submit_Click(object sender, EventArgs e)
{
String username;
username = uname.Text;
String pass;
pass = password.Text;
String query = "insert into student_info values ('" + username + "','" + pass + "')";
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Records Sucessfully saved in the Database");
}
private void update_Click(object sender, EventArgs e)
{
String username;
username = uname.Text;
String pass;
pass = password.Text;
String query = "UPDATE student_info SET password='"+pass+"' WHERE
User_name='"+username+"'";
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
dataGridView1.DataSource = ds.Tables[0];
con.Close();} } }
OUTPUT:-
OUTPUT:-
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:-
Dept. of MCA, BECBGK 2020-21 Page 55
.Net Technologies Laboratory (PCA552L) 2BA18MCA01