c sharp basic questions and answers

c sharp basic questions and answers

Q1.Suppose that you have an interface named IMyinterface in which four methods are available namedmethod1 (), method2 (), method3 (), method4 () and there are two classes named Base and Derived. As a c# developer you have to implement method1 () and method2 () in class Base and remaining two in Derived. How will you do that give example?

There is basic rule of interface that if any class implements the interface, that class should provide the definition of all the method of interface.But for solving the above scenario if we declare one class as abstract class then it is possible. Here is the example.
using System;

namespace CareerRideTest
{
interface IMyinterface
{
void method1();
void method2();
void method3();
void method4();

}
abstract class Base : IMyinterface
{
public void method1()
{
Console.WriteLine(“method1”);
}
public void method2()
{
Console.WriteLine(“method2”);
}
public abstract void method3();
public abstract void method4();
}
class Derived : Base
{
public override void method3()
{
Console.WriteLine(“method3”);
}
public override void method4()
{
Console.WriteLine(“method4”);
}
}
class MainClass
{

static void Main(string[] args)
{
Derived obj = new Derived();
obj.method1();
obj.method2();
obj.method3();
obj.method4();
Console.ReadKey();
}
}
}

Output
method1
method2
method3
method4

Q2.Trace the output for the following program.

using System;

namespace CareerRideTest
{

private class Demo
{
public void show()
{
Console.WriteLine(“Access Modifiers”);
}
}

class MainClass
{

static void Main(string[] args)
{
Demo obj = new Demo();
obj.show();
Console.ReadKey();
}
}
}

Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal. In a namespace class can be declared as public or internal. By default class access specifier is internal.

Q.3Trace the output for the following program.

using System;

namespace CareerRideTest
{

class A
{

public A()
{
Console.WriteLine(“I am in A”);
}
}
class B : A
{
public B()
{
Console.WriteLine(“I am in B”);
}
}
class C : B
{
static C()
{
Console.WriteLine(“I am in Static C”);
}
public C()
{
Console.WriteLine(“I am in C”);
}
}

class MainClass
{
static void Main(string[] args)
{
C obj = new C();

Console.ReadKey();

}
}

}

Output

I am in Static C
I am in A
I am in B
I am in C

Description: Constructors are always executed from base class to derived class, but if the constructor is static then it executes first when the class is loaded in the memory. Always remember that if all the constructor are static then it executes from derived class to base class or bottom to top otherwise it will executes from base class to derived class.

Q.4Trace the output for the following program.

using System;

namespace CareerRideTest
{

class A
{

static A()
{
Console.WriteLine(“I am in A”);
}
}

class MainClass
{
static void Main(string[] args)
{
A obj = new A();

Console.ReadKey();

}
}

}

Ans.This program will not compile. It will give error: access modifiers are not allowed on static constructors. So always remember that you cannot provide access modifiers on static constructors.

Q.5 Suppose that you have two interface named IFirstInterface and ISecondInterface.In both the interface same name method SameMethod() is available .As a developer you have to implement SameMethod() separately . How will you do that give example?

using System;
namespace CareerRideTest
{
public interface IFirstInterface
{
void SameMethod();
}

public interface ISecondInterface
{
void SameMethod();
}
class A : IFirstInterface, ISecondInterface
{
public A()
{
Console.WriteLine(“I am in class A constructor “);
}

void IFirstInterface.SameMethod()
{
Console.WriteLine(“I am in FirstInterface”);
}
void ISecondInterface.SameMethod()
{
Console.WriteLine(“I am in SecondInterface”);
}
}

class MainClass
{
static void Main(string[] args)
{
IFirstInterface objFirst = new A();
objFirst.SameMethod();

ISecondInterface objSecond = new A();
objSecond.SameMethod();
Console.ReadKey();

}
}
}

Output:
I am in class A constructor
I am in FirstInterface
I am in class A constructor
I am in SecondInterface

Note:You cannot write as A objFirst = new A ();
in above program it will give error:

Q.6 Describe delegate in detail.

A delegate is an object that can refer to a method. It means that it can hold a reference to a method. The method can be called through this reference or we can say that a delegate can invoke the method to which it refers. The same delegate can be used to call different methods during the runtime of a program by simply changing the method to which the delegate refers. The main advantage of a delegate is that it invokes the method at run time rather than compile time. Delegate in C# is similar to a function pointer in C/C++.
A delegate type is declared using the keyword delegate. The prototype of a delegate declaration is given below:

delegate ret-type name(parameter-list);

Important Note:A delegate can call only those methods that have same signature and return type as delegate.
Example:
delegate string StrDemo(string str);
This delegate can only call those methods that have string return type and take argument as string.

Example on Delegate.
using System;
// Declare a delegate type.
namespace Test
{
delegate string StrDemo(string str);
class DelegateDemo
{
// Replaces spaces with **.
public string ReplaceSpaces(string s)
{
Console.WriteLine(“Replacing spaces with *.”);
return s.Replace(‘ ‘, ‘*’);
}
// Remove spaces.
public string RemoveSpaces(string s)
{
string temp = “”;
int i;
Console.WriteLine(“Removing spaces.”);
for (i = 0; i < s.Length; i++)
if (s[i] != ‘ ‘) temp += s[i];
return temp;
}

}
class MyClass
{
static void Main()
{
DelegateDemo obj = new DelegateDemo();
StrDemo strOp = new StrDemo(obj.ReplaceSpaces);
string str;
str = strOp(“This is a test.”);
Console.WriteLine(“Resulting string: ” + str);
Console.WriteLine();
strOp = new StrDemo(obj.RemoveSpaces);
str = strOp(“This is a test.”);
Console.WriteLine(“Resulting string: ” + str);
Console.WriteLine();

Console.ReadLine();
}
}
}

Output:

Replacing spaces with *.
Resulting string: This*is*a*test.
Removing spaces.
Resulting string: Thisisatest.

Q.7 Describe Anonymous functions in detail.

Ans: Anonymous means no name. An anonymous function is, an unnamed block of code that is passed to a delegate constructor. One advantage of using an anonymous function is that there is no need to declare a separate method whose only purpose is to be passed to a delegate. An anonymous method is created by following the keyword delegate with a block of code.
Example:
using System;

namespace Test
{
delegate void AnonymousDemo();
class Demo
{
static void Main()
{

AnonymousDemo DelObj = delegate
{
// This is the block of code passed to the delegate.

Console.WriteLine(“Hello anonymous method”);
}; // notice the semicolon
DelObj();
}
}
}

Q.8 Describe Lambda Expressions in brief.

Ans. An anonymous function can be created in another way that is called Lambda Expressions. The lambda expression provides a powerful alternative to the anonymous method.

The Lambda Operator

All lambda expressions use the new lambda operator, which is denoted by the symbol =>. This operator divides a lambda expression into two parts. On the left the input parameter (or parameters) is specified. Right hand side is the body of the lambda. The => operator is sometimes pronounced as “goes to” or “becomes.” There are two types of lambda expression in C#. What type of lambda expression is created, it is determines by the lambda body. The two types are

• Expression lambda
• Statement lambda

An expression lambda contains single expression. In this case, the body is not enclosed between braces. Statement lambda contains block of statements enclosed by braces. A statement lambda can contain multiple statements and include loops, method calls, and if statements.

Example:
using System;
namespace Test
{
delegate int Increment(int n);
// a delegate that takes an int argument

class SimpleLambdaDemo
{
static void Main()
{
// Create an Increment delegate instance that refers to
// a lambda expression that increases its parameter by 3.
Increment incr = count => count + 3;
// Now, use the incr lambda expression.
Console.WriteLine(“Use increment lambda expression: “);
int x = 10;
while (x <= 20)
{
Console.Write(x + ” “);
x = incr(x); // increase x by 3
}
Console.WriteLine(“\n”);

}
}
}

Q.9 Describe Event in .Net.

Ans.It is an important feature of C# that is based upon delegate. An Event is a message sent by an object such as control (Button, Checkbox, Radio button,etc.) or other part of the user interface, that the program responds to by executing code. It is an automatic notification that some action has occurred.
Events are members of a class and are declared using the eventkeyword. The general prototype of an event declaration

event event-delegate event-name;

Example:

using System;
namespace Test
{
// Declare a delegate type for an event.
delegate void MyEventHandler();
// Declare a class that contains an event.
class MyEvent
{
public event MyEventHandler SomeEvent;
// This is called to fire the event.
public void OnSomeEvent()
{
if (SomeEvent != null)
SomeEvent();
}
}
class EventDemo
{
// An event handler.
static void MyHandler()
{
Console.WriteLine(“Event occurred”);
}
static void Main()
{
MyEvent evt = new MyEvent();
// Add MyHandler() to the event list.
evt.SomeEvent += MyHandler;
// Fire the event.
evt.OnSomeEvent();
}
}
}

Q.10 Describe multicast event handling with example.

Ans

using System;
namespace Test
{
// Declare a delegate type for an event.
delegate void MyEventHandler();
// Declare a class that contains an event.
class MyEvent
{
public event MyEventHandler SomeEvent;
// This is called to fire the event.
public void OnSomeEvent()
{
if (SomeEvent != null)
SomeEvent();
}
}
class ButtonClass
{
public void Buttonhandler()
{
Console.WriteLine(“Event received by button object”);
}
}
class CheckBoxClass
{
public void Chkhandler()
{
Console.WriteLine(“Event received by checkbox object”);
}
}
class EventDemo
{
static void Handler()
{
Console.WriteLine(“Event received by EventDemo”);
}
static void Main()
{
MyEvent evt = new MyEvent();
ButtonClass btnObj = new ButtonClass();
CheckBoxClass chkObj = new CheckBoxClass();
// Add handlers to the event list.
evt.SomeEvent += Handler;
evt.SomeEvent += btnObj.Buttonhandler;
evt.SomeEvent += chkObj.Chkhandler;
// Fire the event.
evt.OnSomeEvent();
Console.WriteLine();
// Remove a handler.
evt.SomeEvent -= btnObj.Buttonhandler;
evt.OnSomeEvent();
}
}
}

Output:

Event received by EventDemo
Event received by button object
Event received by checkbox object

Event received by EventDemo
Event received by checkbox object

Important points regarding event.

Events can be specified in interfaces. Implementing classes must supply the event.
Events can be specified as abstract
An event can be specified as sealed.
An event can be virtual.

READ ABOUT HOW TO INSTALL PHP MANUALLY