R4R
Right Place For Right Person TM
 
R4R C# C# FAQS C# Interview Questions and Answers

 


Tolal:418 Click: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Previous Home Next

C# Interview Questions And Answers

Page 1

Ques: 1 Explain Exception handling 

Ans:
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.

Ques: 2 
Explain exception handling in C#?
Ques: 3 
What is Event?

Ans:
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.

Ques: 4 
What is indexer?

Ans:
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 PAGE\n"); 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 Kumar\n"); Console.WriteLine("\n"); Console.ReadLine(); } } }

Ques: 5 
What is Methods?

Ans:
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);

Ans:
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);

Ques: 6 
What is Static classes?

Ans:
Static Class:-These are simply declared using the static modifier. They cannot be derived from or instantiated, and they have no constructors . Their members must all be static. Example:- using System; public static class staticexample { public static void Foo() { Console.WriteLine ("Hello"); } } // Uncommenting this creates a compile-time error, // as static classes can't be derived from // public class DerivationAttempt :staticexample{} class Test { static void Main() { staticexample.Foo(); } }

Ques: 7 
What is Aliases?

Ans:
Aliases:-C# 2.0 introduces the concept of an "alias". This allows you to effectively name an assembly reference when you compile the code, and use that name to disambiguate between names. As well as disambiguating between identical namespace-qualified names, aliases allow you to disambiguate between names which have been declared within an already used namespace and names which belong to the "root" namespace. This is achieved with the predefined alias of global. Example:- using System; namespace Foo.Bar { public class Baz { public static void SayHiLib() { Console.WriteLine ("Hello Lib"); } } } Baz.cs: using System; namespace Foo.Bar { public class Baz { public static void SayHiNested() { Console.WriteLine ("Hello Nested"); } } } class Baz { public static void SayHiBaz() { Console.WriteLine ("Hello Baz"); } } Test.cs: extern alias X; namespace Foo.Bar { class Test { public static void Main() { Baz.SayHiNested(); global::Baz.SayHiBaz(); X::Foo.Bar.Baz.SayHiLib(); } } } Compile: csc /target:library Lib.cs csc /r:X=lib.dll Baz.cs Test.cs

Ques: 8 
What is Partial types?

Ans:
Partial Type:-C# 2.0 introduces the concept of a partial type declaration. This is quite simply a single type which spans multiple files, where each file declares the same type using the partial modifier. The files may refer to members declared within one another without problem (just as forward references within C# is already not a problem). Example:- 1st Part-- partial class Test { string name; static void Main() { Test t = new Test("C# 2.0"); t.SayHello(); } } 2ND PART:- using System; partial class Test { Test(string name) { this.name = name; } void SayHello() { Console.WriteLine ("Hi there. My name is {0}.", name); } Compile with: csc Test1.cs Test2.cs Results: Hi there. My name is C# 2.0.

Ques: 9 What is Method Overloading ?

Ans:
Method Overloading:-A method is considered to be an overloaded method, if it has two or more signatures for the same method name. These methods will contain different parameters but the same return types.It become within the class. A simple example for an overloaded methods are: Public void sum(int a, params int[] varParam); Public void sum(int a);

Ques: 10 Wat's Property?

Ans:
PROPERTY:-Properties provide the opportunity to protect a field in a class by reading and writing to it through the property it is a special method that can return a current object?s state or set it. Simple syntax of properties can see in the following example: public int Old { get {return m_old;} set {m_old = value;} } public string Name { get {return m_name;} Example:- public class prop { private int age; public int humanAge { get //get accessor method { return age; } set //set accessor method { age = value; } } } So when in main program(C#) user wants to use these set and get method to set and get person age. They will use it as Perop boyAge = new Perop(); //create instance of object boyAge. humanAge = 10; // set the age of boy by using setter method of object int bAge = boyAge. humanAge;// get the age of boy by // using getter method of object Console.WriteLine(" Boy age is {0}"+ bAge ); Class Perop is property holder class. In which humanAge is property implementation. value : In set method value variable is internal c# variable. User no needs to define it explicitly.

Ques: 11 
Wat's feature of delegate?

Ans:
Features of delegates: * A delegate represents a class. * A delegate is type-safe. * We can use delegates both for static and instance methods * We can combine multiple delegates into a single delegate. * Delegates are often used in event-based programming, such as publish/subscribe. * We can use delegates in asynchronous-style programming. * We can define delegates inside or outside of classes.

Ques: 12 
What's Delegate?

Ans:
Delegate:- delegate is a type-safe object that can point to another method (or possibly multiple methods) in the application, which can be invoked at later time. when you want to create a delegate in C# you make use of delegate keyword. A delegate type maintains three important pices of information : 1. The name of the method on which it make calls. 2. Any argument (if any) of this method. 3. The return value (if any) of this method. signature of delegate:- Access modifier delegate returntype delegatename (passing value must be same as a function ); public delegate int DelegateName(int x, int y); Example:- namespace MyFirstDelegate { public delegate int MyDelegate(int x, int y); public class MyClass { public static int Add(int x, int y) { return x + y; } public static int Mul(int x, int y) { return x * y; } } class Program { static void Main(string[] args) { MyDelegate del1 = new MyDelegate(MyClass.Add); int addResult = del1(5, 5); Console.WriteLine("5 + 5 = {0}\n", addResult); MyDelegate del2 = new MyDelegate(MyClass.Mul); int multiplyResult = del2(5, 5); Console.WriteLine("5 X 5 = {0}", multiplyResult); Console.ReadLine(); } } }

Ques: 13 
What's REF Keyword?

Ans:
REF:-A reference type and sending a parameter by reference are two different things. If you send a reference type to a method (passed by value), then the reference is passed by value. Since you are sending a reference, you can still access the same object outside of the method using another variable set to the same reference. Now, if you pass that reference by ref, you can modify the value of that parameter so that it holds a new reference to a new object. When you pass it by value, you can't do that. Example:- Using System; class Color { public Color() { this.red = 255; this.green = 0; this.blue = 125; } protected int red; protected int green; protected int blue; public void Getscol(ref int red, ref int green, ref int blue) { red = this.red; green = this.green; blue = this.blue; } } class RefTest1App { public static void Main() { Color color = new Color(); int red; int green; int blue; color.Getscol(ref red, ref green, ref blue); Console.WriteLine("red = {0}, green = {1}, blue = {2}", red, green, blue); } }

Ques: 14 
What's OUT Keyword?

Ans:
OUT:-out keyword is used for passing a variable for output purpose. It has same concept as ref keyword, but passing a ref parameter needs variable to be initialized while out parameter is passed without initialized. It is useful when we want to return more than one value from the method. Example of “out keyword”: class Test { public static void Main() { int a; fun(out a); Console.WriteLine("The value of a is " + a); } public static void fun(out int i) { i=4; //must assigned value. } } The program will result : The value of a is 4

Ques: 15 
what's Param?

Ans:
Params:-The params keyword is used to describe methods that can receive any number of parameters of the specified type, and it results in extra allocations and reduced performance but improves the flexibility of the calling patterns. The main use of the params keyword is to give the ability to create functions that take a variable number of arguments. Example:- Public int sumnumber(params int[] list) { int sum = 0; foreach (int i in list) sum += i; return sum; } Public static void Main() { int ans1 = sumnumber(1); int ans2 = sumnumber(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); int ans3 = sumnumber(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); int ans4 = sumnumber() } OUTPUT:- 1 55 55 0

Ques: 16 
Is there an easy way to get help about an object's member

Ans:
Absolutely. Visual C#'s context-sensitive Help extends to code as well as to visual objects. To get help on a member, write a code statement that includes the member (it doesn't have to be a complete statement), position the cursor within the member text, and press F1. For instance, to get help on the int data type, you could type int, position the cursor within the word int, and press F1.

Ques: 17 
Is it possible for objects that don't have an interface to support events?

Ans:
Yes. To use the events of such an object, however, the object variable must be dimensioned a special way or the events aren't available.

Ques: 18 
Is it possible to create custom events for an object

Ans:
Yes, you can create custom events for your own objects and you can also create them for existing objects. Creating custom events,

Ques: 19 
What’s an interface class?

Ans:
It’s an abstract class with public abstract methods all of which must be implemented in the inherited classes.

Ques: 20 
Can you override private virtual methods?

Ans:
No, moreover, you cannot access private methods in inherited classes, have to be protected in the base class to allow any sort of access.


Goto Page:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Share |

C# Objective

C# Objective Questions And Answers

C# Interview Questions And Answers

C# Subjective Questions And Answers

R4R,C# Objective, C# Subjective, C# Interview Questions And Answers,C#,C# Interview,C# Questions ,C# Answers

New Updates

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R