Assignment - 2:: 1. Explain Main Method With Example
Assignment - 2:: 1. Explain Main Method With Example
Assignment - 2:: 1. Explain Main Method With Example
Assignment - 2:
1. Explain main() method with example.
Ans. The Main method is the entry point of a C# application. (Libraries and services do not require a
Main method as an entry point.) When the application is started, the Main method is the first method
that is invoked. There can only be one entry point in a C# program.
Code:
//tt1-8
class tt1_8
{
private static void Main(string[] args)
{
//tt1-8
string s1;
Console.Write("\n\tInput : ");
s1 = Console.ReadLine();
if (s1 != null)
{
Console.WriteLine("\tOUTPUT : {0}",s1.ToUpper());
}
Console.ReadLine();
}
}
Output:
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
Ans. Like other programming languages, array in C# is a group of similar types of elements that have
contiguous memory location. In C#, array is an object of base type System.Array. In C#, array index
starts from 0. We can store only fixed set of elements in C# array.
1. Single Dimensional
2. Multidimensional Array
3. Jagged Array
Code:
//Array-defination
using System;
class Array_def
{
public static void Main(string[] args)
{
int[] arr = new int[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
Output:
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
Ans.
Code:
//Call By Value
using System;
class Program
{
static void Change(int a, int b)
{
a = 100;
b = 200;
}
public static void Main(string[] args)
{
Console.Write("Enter no 1 : ");
int x = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter no 2 : ");
int y = Convert.ToInt32(Console.ReadLine());
Change(x, y);
Console.WriteLine("After Swap value of a is {0} and b is {1}", x, y);
}
}
Output:
//Call by Reference
using System;
class Program
{
static void Change(ref int a, ref int b)
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
{
a = 100;
b = 200;
}
public static void Main(string[] args)
{
Console.Write("Enter no 1 : ");
int x = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter no 2 : ");
int y = Convert.ToInt32(Console.ReadLine());
Change(ref x, ref y);
Console.WriteLine("After Swap value of a is {0} and b is {1}", x, y);
}
}
Output:
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
Ans. Arguments that are passed by command line known as command line arguments. We
can send arguments to the Main method while executing the code. The string args variable
contains all the values passed from the command line. In the following example, we are
passing command line arguments during execution of program.
Code:
Output:
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
Ans. Class and Object are the basic concepts of Object-Oriented Programming which
revolve around the real-life entities. A class is a user-defined blueprint or prototype from
which objects are created. Basically, a class combines the fields and methods (member
function which defines actions) into a single unit. In C#, classes support polymorphism,
inheritance and also provide the concept of derived classes and base classes.
Code:
class program
{
public static void Main()
{
A1 obj1 = new A1();
Console.Write("Enter any number : ");
obj1.X = Convert.ToInt32(Console.ReadLine());
Console.Write("You have entered : " + obj1.X);
}
}
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
Output:
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
Ans. In C#, inheritance allows us to create a new class from an existing class. It is a key
feature of Object-Oriented Programming (OOP). The class from which a new class is created
is known as the base class (parent or superclass). And, the new class is called derived class
(child or subclass). The derived class inherits the fields and methods of the base class. This
helps with the code reusability in C#.
Code:
//Inheritance
using System;
class A1
{
public string name;
public int age;
public void get_info(string name, int age)
{
this.name = name;
this.age = age;
}
}
class B1 : A1
{
public void display_info()
{
Console.Write("Your name is : {0}", name);
Console.Write("\nYour age is : {0}", age);
}
}
class program
{
public static void Main(string[] args)
{
B1 obj1 = new B1();
Console.Write("Enter your name : ");
string s = Console.ReadLine();
Console.Write("Enter your name : ");
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
int a = Convert.ToInt32(Console.ReadLine());
obj1.get_info(s, a);
obj1.display_info();
}
}
Output:
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
//Method Overriding
using System;
class A1
{
public void show()
{
Console.WriteLine("A1 class");
}
}
class B1 : A1
{
new public void show()
{
Console.WriteLine("B1 class");
}
}
class program
{
public static void Main()
{
B1 obj = new B1();
obj.show();
obj = new A1();
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
obj.show();
}
}
Output:
//Method Overloading
using System;
class A1
{
public void Area(int l, int b)
{
int area = l * b;
Console.Write("The area of Reactangle is : {0}", area);
}
}
class B1 : A1
{
public void Area(float r)
{
double pie = 3.14;
Console.Write("\nThe area of circle is : {0}", (pie * r * r));
}
}
class program
{
public static void Main(string[] args)
{
B1 obj1 = new B1();
obj1.Area(5, 6);
obj1.Area(4);
}
}
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
Output:
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
Ans. C# also provides a way to use short-hand / automatic properties, where you do not have to
define the field for the property, and you only have to write get; and set; inside the property. The
following example will produce the same result as the example above. The only difference is that
there is less.
Code:
class program
{
public static void Main()
{
A1 obj1 = new A1();
Console.Write("Enter any number : ");
obj1.X = Convert.ToInt32(Console.ReadLine());
Console.Write("You have entered : " + obj1.X);
}
}
Output:
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
Ans. A method can also be sealed, and in that case, the method cannot be overridden.
However, a method can be sealed in the classes in which they have been inherited. If you
want to declare a method as sealed, then it has to be declared as virtual in its base class.
Code:
class Program
{
static void Main(string[] args)
{
SealedClass slc = new SealedClass();
int total = slc.Add(6, 4);
Console.WriteLine("Total : " + total.ToString());
}
}
Output:
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
Ans. A static variable is declared with the help of static keyword. When a variable is declared
as static, then a single copy of the variable is created and shared among all objects at the
class level. Static variables are accessed with the name of the class, they do not require any
object for access. The this keyword refers to the current instance of the class and is also used
as a modifier of the first parameter of an extension method.
Code:
class B1 : A1
{
public void display_info()
{
Console.Write("Your name is : {0}", name);
Console.Write("\nYour age is : {0}", age);
}
}
class program
{
public static void Main(string[] args)
{
B1 obj1 = new B1();
Console.Write("Enter your name : ");
string s = Console.ReadLine();
Console.Write("Enter your name : ");
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
int a = Convert.ToInt32(Console.ReadLine());
obj1.get_info(s, a);
obj1.display_info();
}
}
Output: