Wednesday 19 August 2015

What is Exception Handling in .Net

        It is a process of stopping the abnormal termination of a program whenever an exception occur and execute the code that  is not related with exception.

Handling an Exception  : 
                If we want to handle an exception code has to enclosed under some special blocks known as try, catch blocks. Which has to used as following

Syntax :
                 try
                    {
                           --statements which will cause an exception
                           -- statements which should not be executed when an exception occurs.
                    }
               catch(<exception> <var>)
                 {
                           -- Statements which should be executed only when an exceptions occurs
                 }
           --- < Multiple catch blocks of required >--
           

Anonymous types in C#.net

        Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. You can create anonymous types by using the new operator together with an object initializer .

Example :  var value = new { salary=1000 , message = "Hi"};

        Anonymous types typically are used in the select clause of a query expression to return a subset of the properties from each in the source sequence.

 Example :
var salary = from sal in db.Employee where sal.empid ==1 select new {sal.salary,sal.Message}

Asp.Net Page life Cycle

      Set of sub-programs executed towards each time of web page processing are called asp.net page life cycle.

Events :

Page pre-init : This will be executed before memory initialization for webpage.

          Eg : Attaching theme, maser page dynamically to webpage is possible using pre-init event procedure.

Page Init :  This will be executed when memory is initialized for web page.

Page Load :  This will be executed when submitted data and controls are loaded.
       Eg : DataBase connectivity, Reading submitted data can be placed within  page load.

Page PreRender : This will be executed before rendering  HTML content for server side control.

Page unload :  This will be executed once memory is released for web page.

   Eg : Database connection closing

What is difference between Cookie and Session in Asp.Net

The differences between Cookie and Session.

Cookie : 

       1. Cookie is client side state management.

       2. Cookie support only plain text representation.

       3. Cookie is having limitations in terms of number and size.

       4. Cookie doesn't provide security.

       5.  Cookies will reduce burden on server compare to session.

       6. No time out for cookie


 Session : 

       1. Session is server side state management.

       2. Session support representing object.

       3. Session doesn't have limitations for number and size.

       4. Session provides security.

       5.Session time out is 20 mins

     

What is Cookie in asp.net

   Cookie is a small amount of memory used by web server within client system.

The purpose is maintaining personal information of client within client system to reduce on server.

Cookie can be classified into 2 types

                       1. In-memory cookie

                       2. Persistant cookie

1. In-Memory Cookie :   The Cookie placed within browse process memory is called in-memory cookie.


  •    It is temporary cookie specific to browser instance. Once browser will be closed, cookie will be erased.
  • Cookie data will be accessible to all the web pages with client request.
  • .Net is providing HttpCookie class for sending cookie or reading cookie.
     Sending Cookie : 

                   HttpCookie obj = new HttpCookie("Name");

                                       obj.value = "100";

                  Response. Appendcookie(obj);

        
     Reading Cookie submitted by client : 

                      HttpCookie obj = null;

                                       obj = Request.Cookie["Name"];

                            if ( obj == null)

                            {
                              // No cookie submitted       
                            }
                            else
                                obj.value ;


2.  Persistant Cookie : 
                                      Cookie placed with in hard disk memory of client system is called persistant cookie. This will be maintained by client system with perticular lifetime.
                             



What is difference between Array and ArrayList in C#.net

Array :


  •  Char[] vowel = new char[];
  • Array is in the System Namespace.
  • The capacity of an array is fixed.
  • Array is a collection of similar items.
  • An Array can have multiple dimensions.
ArrayList : 

  •  ArrayList alist = new ArrayList();
  • ArrayList is in the system.Collections namepace.
  • ArrayList can increase and decrease size dynamically.
  • Arraylist is a collection of dis-similar items.
  • ArrayList always has exactly one dimentions.


What is difference between the Delete and Truncate in Sql server

Delete :
  • Delete can delete all rows or particular rows .
  • Delete is the DML Command. (DML Stands for Data manipulation language)
  • Delete is recorded in log file.
  • Data can be restored.
  • Delete will not reset Identity.
   Syntax: delete from <tablename> [where <cond>]


    examples: 
  •   delete * from emp . (delete all the rows from emp table)
  •   delete * from emp where job in ('clerk','manager')  (Delete the employee record working as      clerk or manager).  
Truncate :
  •  Truncate command is used to release memory allocated for the table.
  •  Truncate can delete  only all rows.
  •  Truncate is not recorded in log file.
  • Data can't be restored.
  • Truncate will reset Identity.
  • truncate is faster than delete
Syntax: 
  •      truncate table<tablename>.
Example :
  •   Truncate table emp       

How to restrict a class not be inherited by any other class

Answer :
                This can be done by declaring class as Sealed.

When we connecting with DB from Front End Application? How many parameters can we pass

We want to communicate with Sql Server  . we can use the connection String . There are Four default parameters , those are User Id, Password, Data Source and DataBase

What is 3-tier Architecture

    Application architecture's : Every application contains 3 different parts in it like:
  1. UI Part
  2. Logic Part ( BL + DL)
  3. Data Source Part
 3-tier or N-tier  Architecture  :  
          In this model all the 3 parts will sit on 3 different machines to execute, on the client machine what we have is only light weight client (UI) which will connect with the logic part residing on server machine that will in turn  connect with the DB server. Maintenance of the software becomes easier in this model when there were more number of client accessing the application.

What is CLI and How many types in .Net

        To develop the .Net Framework , Microsoft has first prepared  a set of specifications known as CLI Specification( Common Language Infrastructure).
                     This specification was standardization under " ISO and ECMA ". ISO stands for international standard Organization and ECMA stands for European Computer manufacture Association.
Note : The CLI specification for Open which gives a chance any one to develop .Net'f Framework.
CLI specification covers the following rules

  1. CLS (Common Language Specification)
  2. CTS ( Common Type System)
  3. BCL  ( Base class Library )
  4. VES ( Virtual execution System )

          ( Or) 
         CLR ( Common Language Runtime)
        

What is the LINQ? How many types of LINQ

         LINQ Stands for Language Integrated Query.  It was a new query language designed by Microsoft  introduced in c# 3.0  (or) .Net 3.5 same as SQL database.
             
             SQL can be used for querying only on SQL databases. where as LINQ is designed to query to Query on collections and Arrays, Sql databases, XML  and Entities.
     LINQ has four classifications to it.

  1. LINQ to Collection
  2. LINQ to SQL
  3. LINQ to XML
  4. LINQ to entities.   

What is Collection Initializer in.Net


Which allows to initialize a collection also at the time of it’s declaration similar to array which was not possible in the earlier version
Before 3.0 :-
                List<int> li = new List<int>();
                Int[] arr={ 10,20,30,40,50,60};
                li.addRange(arr);
From 3.0 :-
                List<int> li = new List<int> { 10,20,30,40,50,60};

What is difference between Value type and Reference Type in .Net

Value type : 

  • The data types which store the data directly into their memory location is known as value type.
  • Memory is allocated at compile time.
  • CLR  doesn't provide automatic memory management.
  • Value types are the fixed length like int,float,char etc...
Reference Types  :

  • The data types which doesn't store the data directly into their memory location rather reference to other memory location  where values are stored.
  • Memory is allocate at run time.
  • CLR provides automatic memory management.
  • Reference types are the variable length like object and string.

How to generate the Fabonacci Series in C#.Net

Example 1:  


                     We can give the value at the time Runtime.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    class fabinacci2
    {
        static void Main()
        {
            int a = -1;
            int b = 1;
            int x;
            Console.WriteLine(" enter the x value ::");
            x = int.Parse(Console.ReadLine());
            for (int i = 0; i < x; i++)
            {
                int sum = b + a;
                a = b;
                b = sum;
                Console.WriteLine(sum);


            }
            Console.ReadLine();
        }
    }
}


OutPut :




Example2 :   fabinacci1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    class fabinacci1
    {
        public void fabincc(int x)
        {
            int a = 0;
            int b = 1;
            for (int i = 0; i < x; i++)
            {
                int result = a + b;
                Console.WriteLine(result);
                a = b;
                b = result;
            }
        }
    }
}




Program.cs :   Value can be taken at the time of compilation.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    class fabinacci
    {
        static void Main()
        {
            fabinacci1 f = new fabinacci1();
            f.fabincc(10);
            Console.ReadLine();


        }
    }
       


    }

OutPut :



Example 3 : 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    class fabonacci
    {
        static void Main()
        {
            Console.WriteLine("ente the value");
            int x = int.Parse(Console.ReadLine());
            for (int i = 0; i < x; i++)
            {
                Console.WriteLine(fabonac(i));
              
            }
            Console.ReadLine();
        }
        static int fabonac(int i)
        {


            if (i <= 0)
            {


                return 0;
            }
            else if (i == 1)
            {
                return 1;
            }
            else
            {
                return fabonac(i - 1) + fabonac(i - 2);
            }


        }
    }
}


Output :

What is an Abstract Class in C#.Net

Abstract Class  :


           An Abstract class can contain both Abstract and Non-Abstract methods. If any child class of the Abstract class wants to consume the Non-Abstract methods under the Abstract class. First it has to provide the implementation for all the abstract methods under the abstract class. 


Note :
             If a class contains any abstract methods in it object of that class can't be created.So object of an abstract class can't be created.


Examples : Absparent.Cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    abstract class Absparent
    {
        public void add(int x, int y)
        {
            Console.WriteLine(x + y);
        }
        public void sub(int x, int y)
        {
            Console.WriteLine(x - y);
        }
        public abstract void mul(int x, int y);
        public abstract void div(int x, int y);
    }




    }


AbsChild.CS



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    class Abschild :Absparent 
    {
        public override void mul(int x, int y)
        {
            Console.WriteLine(x * y);
        }
        public override void div(int x, int y)
        {
            Console.WriteLine(x / y);


        }
        static void Main()
        {
            Abschild c = new Abschild();
            c.add(100, 500);
            c.sub(200, 100);
            c.mul(100, 100);
            c.div(200, 100);
            Console.ReadLine();
        }  
    }
}

Output : 

      600
      100
      10000
      2

 Another Example :


                            Using abstract class we can provide re-usability to multiple classes and also impose restriction on child class.
                             In the below example the class AbstractFigure.cs the class all the variables that are required under various child classes like Rectangle, Triangle, Circle , cone etc.... while can be consumed under the child classes  the provide re-usability. 
                            It also imposes a restriction on child classes to provide the implementation for the abstract method it has defined. Which should be done by each child according to it's requirement without change the signature.


------> Add a class AbstractFigure.CS and write the following 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
   public abstract  class AbstractFigure
    {
       public double width, height, radius;
       public abstract double GetArea();
       public abstract double GetPerimeter();
   }


    }


------> Add a class Rect.cs and Write the following 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
  public class Rect : AbstractFigure  
    {
      private double p;


      public Rect(double width, double height) 
      {
          // In this context this and base refers to variables of AbstractFigure calss only
          this.width = width;
          base.height = height;
      }


      public Rect(double p)
      {
          // TODO: Complete member initialization
          this.p = p;
      }


      public override double GetArea()
      {
          return width * height;
    
      }
      public override double GetPerimeter()
      {
          return 2 * (width + height);


      }
     
      }


    }
  ----> Add a new class TestFigure.cs to consume all the figures we have implemented


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    class TestFigure
    {
        static void Main()
        {
            Rect r = new Rect(347.87,332.34);
            Console.WriteLine(r.GetArea());
            Console.WriteLine(r.GetPerimeter());
            Console.ReadLine();
        }


    }
}


OutPut :

      115611.1158
      1368.42          

What is DOM Parser

 DOM STANDS FOR Document Object Model
   DOM Parser can parse XML or HTML source stored in a string into a DOM document.DOM Parser can be specified in DOM Parsing and Serialization.
        To Create DOM Parser use 
new DOMParser()

What is difference between Dataset and DataReader in C#.net

Dataset :

  •    Read/Write Access
  • Supports multiple tables from difference databases.
  • It is comes under Disconnected Mode.
  • Bind to multiple controls.
  • Forward and Backward of scanning of data.
  • Slower access to Data.
DataReader :

  • Read only Access.
  • Supports a single table based on a single Query of one Database.
  • It is comes under Connection Mode.
  • Bind to single control.
  • Forward only scanning of data.
  • Faster access to data.


String Reverse Program and Alternative characters in reverse using C#.Net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Biggestvalueinarray
{
    class Class1
    {
        static void Main()
        {
            string str = "Mahesh Babu";
            char[] arr = str.ToCharArray();
            string str1="",str2="";
            for (int i = arr.Length-1; i >= 0; i--)
            {
                str1 = str1 + arr[i].ToString ();
            }
            Console.WriteLine(str1);
            char[] arr1 = str1.ToCharArray();
            int len=str1.Length ;
            for (int i =0; i<arr1.Length ;i+=2)
            {
                str2 = str2 + arr1[i].ToString();
            }
            Console.WriteLine(str2);
            Console.Read();
        }
    }
}

Write a program special Star Triangle using C#.Net


Program:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
    class specialprogram
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the value:");
            int x = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= x; i++)
            {
                for (int j = 1; j <= x - i; j++)
                {
                    Console.Write(" ");
                }
                for (int k = 1; k <= 2 * i - 1; k++)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("*");
                    Thread.Sleep(100);
                }
                Console.WriteLine();
            }
        }
    }
}

output:



What is an inheritance in .Net with Examples

  Defination:        
                      Acquiring the properties of  base class into the derived class is called as inheritance.
Syntax: 
              [<modifiers>] class <cname> : <parentclassname>


eg:                               class class1
                                     {
                                        ---- members-----
                                      }
                                       class class2 : class1


There are Two type of Inheritance : 
                                                     As for the standards of Object-oriented programming There are two types Inheritance.

  1. Single Inheritance
  2. Multiple Inheritance
Single Inheritance : 
                                    If a class has only on immediate parent class to the it's known as Single inheritance.


                       class 1---------->class2-------->class3--------->class4


 Multiple Inheritance :
                                 If a class has more than one immediate parent class to it . It's known as Multiple Inheritance.


                                      class1                       class2
                                           |                                   |
                                           |-------> class3 <--- |


Note :
                Inheritance allows to consume members of the parent under child. But not private members. Default scope for members of a class was private only.


----> Add a class inherit.cs write the following 


                using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    class inherit
    {
        public inherit()
        {
            Console.WriteLine("inherit constructor");
        }
        public void test1()
        {
            Console.WriteLine("method one");
        }
        public void test2()
        {
            Console.WriteLine("method two");
        }


    }
}

-----> Add a new  class inherit1.cs write the following 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class inherit1 : inherit 
    {
        public inherit1()
        {
            Console.WriteLine("inherit1 constructor");
        }
        public void test3()
        {
            Console.WriteLine("Method three");
        }
        static void Main()
        {
            inherit1 i = new inherit1();
            i.test1();
            i.test2();
            i.test3();
            Console.ReadLine();
        }
    }
}

Output : 

Explanation of the Inheritance :
  •    In Inheritance execution of a child class always starts by invoking the default constructor of parent class. Which should be accessible to the child. It not accessible inheritance will not be possible.
  • Members of the parent class can be accessed by child classes where as child class members can't be accessed by parent class. Because a parent class is not aware it is child classes.
                      We test this write the following code under the child class main method.
                                 inherit p = new inherit(); // here p is parent object.
                                   p.test1();  p.test2();  
                                   p.test3();  // here test3() is child class method can't be accessible. i.e Invalid
  •   AS we aware that object of a class can be assigned to the variable of the same class. It can also be assigned to variable of it's parent class and make it was a reference . By using that reference we can't invoke members of child class. 
                      To test this rewrite the code under Main method of child class inherit1 as following.

                                 inherit1 i = new inherit1();
                                   inherit1 p =i; // p is not object . this is reference to child class.
                                   p.test1();
                                   p.test2();
                                   p.test3();     // invalid

Note : the  object of the child class that is present as parent 's reference. If required can be converted by into child's reference. But should be done by using explicit type casting.
                            inherit1 i = new inherit1();
                               inherit p = i;;
                              inherit1 i1 = (inherit1)p;
                                           or 
                              inherit1 i1 = p as inherit1;
                                i1.test1(); i1.test2(); i1.test3();
Note : using child reference we can assign call all the methods.
  • Every class what we define in .Net languages has a default parent class. Even if we inheritance or not. i.e the class object. when we compile a program a compiler verify whether the class is inheriting from another class. If not inheriting compiler inherits from the class object .So object class will be a parent either directly or indirectly for any class.
                          object----------->inherit-------------> inherit1
                   The member of object class can be accessed from any class what we are going define.
 eg:       GetType();    ToSting() etc

 -----> To check this rewrite the code under main method of class to as following
               
                  object obj = new object();
                  Console.WriteLine(obj.GetType());
                   inherit p = new inherit();
                   Console.WriteLine(p.GetType());
                   inherit1 c = new inherit1();
                   Console.WriteLine(c.GetType());


Another Example of Inheritance : 

parent class :

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class inherit
    {
        public inherit()
        {
            Console.WriteLine("inherit constructor");
        }
        public void test1()
        {
            Console.WriteLine("method one");
        }
        public void test2()
        {
            Console.WriteLine("method two");
        }

    }
}
---------------------------------------------------------------------------------
Child Class :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class inherit1 : inherit 
    {
        static  inherit1()
        {
            Console.WriteLine("inherit1 constructor");
        }
        public void test3()
        {
            Console.WriteLine("Method three");
        }
        static void Main()
        {
            inherit1 i = new inherit1();
            i.test1();
            i.test2();
            i.test3();
            Console.ReadLine();
        }
    }
}

Output :

Explanation :
        
                   Here Execution of the class starts from Static Constructor method  of any parent or child class. If no Static Constructor  then execution starts from Default Constructor of the parent class can executed. In a class Static Constructor or Static Main() should be there executed. 

Some Steps for Execution Starts from :
  1. Static Constructor Method    Note :  // here parent  or child class constructor first executed . If both constructor are Static . First Execute the Child Class Static Constructor and Static Main method (if any methods in Main() method).
  2. Parent class Methods.
  3. Child class   Methods.

What is difference between instance constructor and Static constructor in C#.Net

Main Differences are :
  •  A Constructor declare using Static modifier was known as  Static Constructor. Rest of all were Instance Constructor.
  • Static Constructor are responsible for initialization of static variables and Instance Constructor for initialization of instance constructor.
  • A static constructor is called only one's in the execution of a class. Where as instance constructor gets called each time we create the object of the class. if no object is created. It is not called at all.
  • static constructor is the first block of which gets executed under the class.
  • A static constructor can't be parameterized because explicit calling of the constructor is not done.
Sample Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class staticconstructor
    {
        static staticconstructor()
        {
            Console.WriteLine("static constructor");
        }
         staticconstructor()
        {
            Console.WriteLine("non-static constructor");
        }
        static void Main()
        {
            Console.WriteLine("main method");
            staticconstructor st = new staticconstructor();
            staticconstructor st1 = new staticconstructor();
            Console.ReadLine();
        }
    }
}

Output :


Factorial Number Program in C#.Net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
   public  class Class1
    {
       static long factorial(long num)
       {
           if(num<=1)
               return 1;
           else
               return num*factorial(num-1);
       }
       static void   Main(string[] args)
       {
           Console.WriteLine("enter the factorial Number::");
           int x =int.Parse(Console.ReadLine());
           Console.WriteLine("the factorial of "+ x +"  is:  {0}", factorial(x));
         //  return 0;
           Console.ReadLine();
     
       }
    }
}

OUTPUT :
  
      enter the factorial Number : : 5
       The factorial of 5 is : 120

What is difference between For loop and Foreach loop in C#.Net

For Loop:
  • In case of for the variable of the loop is always be int only.
  • The For Loop executes the statement or block of statements repeatedly until specified expression evaluates to false.
  • There is need to specify the loop bounds(Minimum, Maximum).
example:  
       
               using sytem;
                 class class1
                 {
                   static void Main()
                    {
                      int j=0;
                       for(int i=0; i<=10;i++)
                         {
                           j=j+1;
                            }
                             Console.ReadLine();
                                }
                           }        


 Foreach  loop :

  •  In case of Foreach the variable of the loop while be same as the type of values under the array.
  • The Foreach statement repeats a group of embedded statements for each element in an array  or an object collection.
  •  You do not need to specify the loop bounds minimum or maximum.
example:  
       
               using sytem;
                 class class1
                 {
                   static void Main()
                    {
                      int j=0;
                       int[] arr=new int[]{0,3,5,2,55,34,643,42,23}; 

                         foreach(int i in arr)
                         {
                           j=j+1;
                            }
                             Console.ReadLine();
                                }
                           }        

What is difference between instance variables and Static variables in C#.Net

Main Difference between them are 
  • A variable declared using static modifier or Declared under a static block were know as static variables. Rest of all were instance variables.
  • A static variable gets initialize once the execution of class starts. Where as instance variable gets initialize only after creating object of class.
  • The Minimum and Maximum number of times an static variable gets initialize will be one and only once. Where as in case Instance it will be Zero or N.
  • As Static variables were only a single copy for the complete class modifications made by one object will be reflected to other. Where as Instance variables were separated copy's for each object changes of one will not reflect to the other.
Note: 
           While referring to instance members we require to refer them by using the object of class. Where as we refer to static members by using class name.

Sample Example of Static variable and Instance variable :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
   public  class staticvariables
    {
        int x = 100;   // instance
        static int y = 200; //static
        static void Main(string[] args)
        {
           int z = 300; // static
            staticvariables obj = new staticvariables();
            Console.WriteLine(staticvariables.y);
            Console.WriteLine(obj.x);
            Console.WriteLine(z);
            Console.ReadLine();
        }
    }
}

OUTPUT :
           200
           100
           300