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: