C#



Totel:418 Click: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
C# Interview Questions And Answers

Page 1

Exception Handling:-# provides built-in support for handling anomalous situations, known as exceptions, which may occur during the execution of your program. These exceptions are handled by code that is outside the normal flow of control. The try, throw, catch, and finally keywords implement exception handling.
Throw:-The throw statement is used to signal the occurrence of an anomalous situation (exception) during the program execution. The throw statement takes the form:
               throw [expression];
where: 

expression :-
The exception object. This is omitted when re-throwing the current exception object in a catch clause.

Try:-
      The try-block contains the guarded code block that may cause the exception. The block is executed until an exception is thrown or it is completed successfully. For example, the following attempt to cast a null object raises the NullReferenceException exception:

object o2 = null;
try
{
   int i2 = (int) o2;   // Error
}
Catch:-
        The catch clause can be used without arguments, in which case it catches any type of exception, and referred to as the general catch clause. It can also take an object argument derived from System.Exception, in which case it handles a specific exception. For example:

catch (InvalidCastException e) 
{
}
Finally:-
         finally is used to guarantee a statement block of code executes regardless of how the preceding try block is exited.
              The finally block is useful for cleaning up any resources allocated in the try block. Control is always passed to the finally block regardless of how the try block exits. The try-finally statement takes the form:

try try-block finally finally-block
where: 

try-block 
Contains the code segment expected to raise the exception. 
finally-block 
Contains the exception handler and the cleanup code.

Event:-The Event model in C# finds its roots in the event programming model that is popular in asynchronous programming. The basic foundation behind this programming model is the idea of "publisher and subscribers."

Example:-
            using System;
using System.IO;

namespace EventExample
{
      public class MyClass
    {
     public delegate void  LHandler(string message);
 
        // Define an Event based on the above Delegate
        public event LHandler Log;
  
        // Instead of having the Process() function take a delegate
        // as a parameter, we've declared a Log event. Call the Event,
                public void Process()
        {
            OnLog("Process() begin");
            OnLog("Process() end");
        }
 
           protected void OnLog(string message)
        {
            if (Log != null)
            {
                Log(message);
            }
        }
    }
 
    // The FLog class merely encapsulates the file I/O
    public class FLog
    {
        FileStream fileStream;
        StreamWriter streamWriter;
 
        // Constructor
        public FLog(string filename)
        {
            fileStream = new FileStream(filename, FileMode.Create);
            streamWriter = new StreamWriter(fileStream);
        }
 
        // Member Function which is used in the Delegate
        public void Logger(string s)
        {
            streamWriter.WriteLine(s);
        }
 
        public void Close()
        {
            streamWriter.Close();
            fileStream.Close();
        }
    }
 
       public class TestApplication
    {
        static void Logger(string s)
        {
            Console.WriteLine(s);
        }
 
        static void Main(string[] args)
        {
            FLog fl = new FLog("process.log");
            MyClass myClass = new MyClass();
 
            // Subscribe the Functions Logger and fl.Logger
            myClass.Log += new MyClass.LHandler(Logger);
            myClass.Log += new MyClass.LHandler(fl.Logger);

            // The Event will now be triggered in the Process() Method
            myClass.Process();
 
            fl.Close();
        }
    }
} 
Compile an test: 
# csc EventExample.cs
# EventExapmle.exe
Process() begin
Process() end
# cat process.log
Process() begin
Process() end 




An event is a placeholder for code that is executed when the event is triggered, or fired. Events are fired by a user action, program code, or by the system.




    The following important conventions are used with events:

        * Event Handlers in the .NET Framework return void and take two parameters.
        * The first paramter is the source of the event; that is the publishing object.
        * The second parameter is an object derived from EventArgs.
        * Events are properties of the class publishing the event.
        * The keyword event controls how the event property is accessed by the subscribing classes.

Indexer:-
                *
      Indexer Concept is object act as an array.
    *
      Indexer an object to be indexed in the same way as an array.
    *
      Indexer modifier can be private, public, protected or internal.
    *
      The return type can be any valid C# types.
    *
      Indexers in C# must have at least one parameter. Else the compiler will generate a compilation error.

Signature:-
          his [Parameter]

{

    get

    {

        // Get codes goes here

    }

    set

    {

        // Set codes goes here

    }

} 
.................................................
Exapmle:-


 using System;

using System.Collections.Generic;

using System.Text;

 

namespace Indexers

{

    class PClass

    {

        private string[] range = new string[5];

        public string this[int indexrange]

        {

            get

            {

                return range[indexrange];

            }

            set

            {

                range[indexrange] = value;

            }

        }

    }

 

    /* The Above Class just act as array declaration using this pointer */

 

    class cclass

    {

        public static void Main()

        {

            PClass obj = new PClass();

 

               obj[0] = "ONE";

            obj[1] = "TWO";

            obj[2] = "THREE";

            obj[3] = "FOUR ";

            obj[4] = "FIVE";

            Console.WriteLine("WELCOME TO C# CORNER HOME PAGEn");

            Console.WriteLine("n");

 

            Console.WriteLine("{0}n,{1}n,{2}n,{3}n,{4}n", obj[0], obj[1], obj[2], obj[3], obj[4]);

            Console.WriteLine("n");

            Console.WriteLine("ALS.Senthur Ganesh Ram Kumarn");

            Console.WriteLine("n");

            Console.ReadLine();

        }

    }

} 

Method:-Method is object-oriented item of any language. All C# programs are constructed from a number of classes and almost all the classes will contain methods. A class when instantiated is called an object. Object-oriented concepts of programming say that the data members of each object represent its state and methods represent the object behavior.
Method Signature:-
Each method is declared as follows:

 Access modifier   Return-type methodname ( Parameterslist );

Example:- public string printpro(string s,int y);

Method:-Method is object-oriented item of any language. All C# programs are constructed from a number of classes and almost all the classes will contain methods. A class when instantiated is called an object. Object-oriented concepts of programming say that the data members of each object represent its state and methods represent the object behavior.
Method Signature:-
Each method is declared as follows:

 Access modifier   Return-type methodname ( Parameterslist );

Example:- public string printpro(string s,int y);


Goto Page:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
C# Objective Questions And Answers

C# Objective Questions And Answers

C# Interview Questions And Answers

C# Subjective Questions And Answers


R4R,C# Objective fresher and experienced, C# Subjective fresher and experienced ,C# Interview Questions And Answers,C# fresher and experienced,C# Interview fresher and experienced ,C# Questions fresher and experienced,C# Answers fresher and experienced