C# notes part ->2




Structure;

A structure is a value type datatype when you want a single variable to hold the related datatypes of various datatypes, we can use a structure.


Eg:
structure StudentDetails
{
int StuID;
string StuName;
string StuAddress;
decimal feez;
}

Enumeration:

it is a value type datatype. Enumeration contains its own values and cannot inherit or pass inheritance. Enumeration allows you to assign symbolic  names to integral constants.

Eg:
       Enum Colors
{
       Red,Green,Maroon,Blue,Yellow,Orange,White
         |       |         |        |       |         |         |
         0       1          2       3       4         5         6           
};
int FirstColor=(int)Colors.Green;
console.WriteLine(FirstColor);       à 1 as output


oops
Advantages:
Ø Software reuse is enhanced.
Ø Software maintenance can be reduced.
Ø Data access is restricted, provided better data security.
Ø Software is easily developed for complex problems.
Ø Software may be developed meeting the requirements on time and on the estimated budget.
Ø Software has improved the performance.
Ø Software quality is improved.
Ø Class hierarchies are helpful in design process allowing increase extensibility.
Ø Modularity is achieved.
Ø Data abstraction is possible.

Fearures of OOPS:

1) Data Abstraction
2) Encapsulation
3) Inheritance
4) Polymorphism

1). Abstraction:
       it is a feature of OOP and it is used to extracting only the relevant information that  means getting necessary data is called as abstraction.

2). Encapsulation:
       it is a feature of OOP and it is used to packaging one or more components together and it involves preventing access to nonessential details. Wrapping the properties and methods into a single container is called as an encapsulation.

OOP methodology:
Object Orientation is software development methodology that is based on modelling a real world system. An OOP consists of classes and objects.

Object: it is nothing but physical instance of a class.
An object has the following charactrristics.
Ø State
Ø Behavior
Ø Identity
Class: class modal an abstraction by defining its states and behavior together.
   Calss

   State
+
Behavior

Syntax:
  <access specifier> class <classname>
{
       //declaration of fields
       //declaration of methods

}
Eg:
     Public class Student
{
       private int StuID;
       public void Display()
       {
              -----
       }
}
   
Access specifiers:
Public, private, protected, internal and  protected internal.
Public:   scope is outside the class.
Private: scope is within the class.
Protected: scope is within the derived class.
Internal: scope is within the same assembly.
Protected internal: scope is within the derived class of within the same assembly.

Instantiation of an object;
  Classname objectname = new classname();
Eg:
   Student s=new Student();
   s.StuID=100;
   s.Display();


using System;
namespace SampleClassEx
{

    public class Student
    {
        public int StuId;
        public string StuName;
        public string StuAddress;
        public void Display()
        {
            Console.WriteLine("Student Id = {0},  Student Name={1},  Student Address = {2}",StuId,StuName,StuAddress);
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            Student s =new Student();
            s.StuId=100;
            s.StuName="Rajitha";
            s.StuAddress="Hyderabad";
            s.Display();
            Console.ReadLine();
        }
    }
}


Constructors

it is an ordinary method which will be invoked whenever we create an object. The main purpose of constructor is to initialize the data to the fields(variables).


Types of constructors:
1.  Default constructors
2.  Parameterized constructors
3.  Copy constructors
4.  Static constructors

Default constructors: The constructor without parameters is called as default  constructor.
Syntax:
public classname()
{
       ……….
}
eg:
public Student()
{
       ……..
}

Parameterized constructors: the constructor which will take the parameters then it is called as parameterized constructor.
Syntax:
Public classname(parameters)
{
       ……
}
eg:
public Student(int StuId)
{
…..
}

Copy constructors:
Syntax:
Public classname(other-classname object-name)
{
……
}
Static constructors:
Syntax:
Static classname()
{
       ….
}



  
using System;
namespace constructorsappex
{

    public class Student
    {
        int sid;
        string sname, saddress;
        public Student()
        {
           
            Console.WriteLine("enter the student details id,name and address");
            sid=Convert.ToInt32(Console.ReadLine());
            sname = Console.ReadLine();
            saddress = Console.ReadLine();
        }
        public Student(int id, string name, string addr)
        {
            sid = id;
            sname = name;
            saddress = addr;
          
        }
        public Student(Student obj)
        {
            sid = obj.sid;
            sname = obj.sname;
            saddress = obj.saddress;        
        }
        public void display()
        {
            Console.WriteLine("the Student ID ={0}, Studet name={1}, Student Address = {2}",sid,sname,saddress);
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Student s = new Student(); //default constructor
            Student s1 = new Student(100,"Rajitha","Hyderabad\n"); //parameterised constructor
            Student s2 = new Student(s1);  //copy constructor
            Console.Clear();
            Console.WriteLine("\n\n*****OUTPUT*****\n\n\n");
            Console.WriteLine("\n\nhai this is default constructor\n");
            s.display();
            Console.WriteLine("\nthis is parameterised constructor\n");
            s1.display();
            Console.WriteLine("\nthis is copy constructor\n");
            s2.display();
            Console.ReadLine();
        }
    }
}




Method
       It is a group of statements which are required to perform a well defined task.

Syntax:
Accessspecifier returntype methodname(parameters)
{
       //set of statements
}

Types of methods:
1.  Ordinary method
2.  Static method

Static method:
Syntax:
Access-specifier static returntype methodname(args)
{
       ……
}
eg:
public static void Display()
{
       ….
}
Parameters type:
1.  Value type parameters( input parameters)
2.  Reference type parameters
3.  Out parameters
4.  Params parameters
Call by value:  the parameters passed by value create a separate copy in the memory. So if you make any changes in the formal parameters which will not be reflected back on the actual parameters. This is default mechanisn in C#.
Reference parameters: the parameters passed by reference do not create a copy of the variable in the memory. A reference parameter stores the memory address of the data member passed. This mechanism support with a “ref” keyword in C#. That means if you make any changes to formal parameters which will be reflected back to actual parameters.
Output parameters: parameters supported by
“out “keyword. Those are used to pass the value out of the method, that means no need to initialize the value where as in the reference parameters it  will be initialized.  No need to use “return” keyword in the function body.
Params parameters: it is used to receive the variable length of argument list at runtime. It supports with “params” keyword.

Program on static and non-static methods:
using System;
namespace MethodAppEx
{

    class Sample
    {
        public  void display()
        {
            Console.WriteLine("this is a non static mathod");
        }
        public static void print()
        {
            Console.WriteLine("this is a  static mathod");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Sample s = new Sample();
            s.display();
            Sample.print();
            Console.ReadLine();
        }
    }
}

Program on call by value



using System;

namespace ParamAppEx
{
    //call by value

    class Program
    {
        static void Main(string[] args)
        {
            int a = 5;
            Console.WriteLine("\n the value of a before calling  {0}  ",a);
            Change(a);
            Console.WriteLine("\n the value of a after calling  {0}  ",a);
            Console.ReadLine();

        }
        public static void Change(int x)
        {
            x = 10;
       
        }
    }
}

Program on call by reference

using System;
namespace ParamAppEx1
{
    class Program
    {
        static void Main(string[] args)
        {
             int a = 5;
            Console.WriteLine("\n the value of a before calling  {0}  ",a);
            change(ref a);
            Console.WriteLine("\n the value of a after calling  {0}  ",a);
            Console.ReadLine();

        }
        public static void change( ref int x)
        {
            x = 10;
       
        }
        }
   
}

Program on out parameters

using System;
namespace ParamAppEx2
{
//out parameters

    class Program
    {
        static void Main(string[] args)
        {
             int a = 5,b=10,c;
           
            Add(ref a,ref b,out c);
            Console.WriteLine("\n the value of Addition   is  {0}  ",c);
            Console.ReadLine();

        }
        public static void Add(ref int x, ref int y, out int z)
        {
           z= x + y;
        }
    }
   
}
program on params parameters

using System;
namespace ParamAppEx3
{

    //params parameters


    class Program
    {
        static void Main(string[] args)
        {
            display(1,2,3,4,5,6,7,8,9);
            display(1,2,3,5,6,7,8);
            display(1,2,3,6,7,8);
            display(1,2,3,7,8);
            display(1,4,5,6,7,8);
            display();
            Console.ReadLine();
           
        }
        public static void  display(params int [] list)
        {
            foreach (int i in list)
            {
                Console.Write(i + "\t");

            }
            Console.WriteLine();
        }
    }
}



Comments

Popular posts from this blog

WCF interview questions

The term 'Connect-MsolService' is not recognized as the name of a cmdlet, function, script file, or operable program

what is Event Cache table in sharepoint