Assignment - 2:: 1. Explain Main Method With Example

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

21SOEIT13014 Enterprise Computing Through .

NET Framework (CE525)

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)

2. Explain Array with suitable example.

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.

There are 3 types of arrays in C# programming:

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;

        Console.Write("The array elements are : ");


        for (int i = 0; i < arr.Length; i++)
        {
            Console.Write(arr[i] + " ");
        }
        Console.ReadLine();
    }
}

Output:
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)

3. Explain methods with pass by value and pass by references examples.

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)

4. Explain command line arguments with example.

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:

//Command Line Arguments


using System;
class command_arg
{
    public static void Main(string[] args)
    {
        Console.WriteLine("\n\tHello, World!");
        Console.WriteLine("\tYou entered the following {0} command line
arguments:", args.Length);
        foreach (Object obj in args)
        {
            Console.WriteLine("\t" + obj);
        }
        Console.ReadLine();
    }
}

Output:
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)
21SOEIT13014 Enterprise Computing Through .NET Framework (CE525)

5. Explain Class and Object with suitable example.

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 and Objects


using System;
class A1
{
    private int x;
    public int X
    {
        get
        {
            return x;
        }
        set
        {
            x = value;
        }
    }
}

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)

6. Explain inheritance with example.

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)

7. Explain Method Overloading and Method Overriding with example.


Ans. Method Overloading is a type of polymorphism. It has several names like “Compile
Time Polymorphism” or “Static Polymorphism” and sometimes it is called “Early Binding”.
Method Overloading means creating multiple methods in a class with same names but
different signatures (Parameters). It permits a class, struct, or interface to declare multiple
methods with the same name with unique signatures.
Method Overriding is a type of polymorphism. It has several names like “Run Time
Polymorphism” or “Dynamic Polymorphism” and sometime it is called “Late Binding”.
Method Overriding means having two methods with same name and same signatures
[parameters], one should be in the base class and other method should be in a derived class
[child class]. You can override the functionality of a base class method to create a same name
method with same signature in a derived class. You can achieve method overriding using
inheritance. Virtual and Override keywords are used to achieve method overriding.
Code:

//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)

8. Explain Get and Set accessors methods with example.

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:

//Get and Set


class A1
{
    private int x;
    public int X
    {
        get
        {
            return x;
        }
        set
        {
            x = value;
        }
    }
}

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)

9. Explain base and sealed with example.

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:

//Base and Sealed


using System;
sealed class SealedClass
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

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)

10. Explain static and this with example.

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:

//Static and This


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:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy