Microsoft .NET Interview Questions and Answers
Ques 41. Are events synchronous of asynchronous?
Asynchronous
Ques 42. Events use a publisher/subscriber model. What is that?
Objects publish events to which other applications subscribe. When the publisher raises
an event all subscribers to that event are notified.
Ques 43. Can a subscriber subscribe to more than one publisher?
Yes, also - here's some code for a publisher with multiple subscribers.
using System;
namespace Console1
{
class Class1
{
delegate void myDelegate(int parameter1);
static event myDelegate myEvent;
static void AStaticMethod(int param1)
{
Console.WriteLine(param1);
}
static void Main(string[] args)
{
MyClass myInstance = new MyClass();
myEvent += new myDelegate(myInstance.AMethod);
myEvent += new myDelegate(AStaticMethod);
myEvent(1); //both functions will be run.
Console.ReadLine();
}
}
class MyClass
{
public void AMethod(int param1)
{
Console.WriteLine(param1);
}
}
}
Another example:
using System;
using System.Threading;
namespace EventExample
{
public class Clock
{
public delegate void TwoSecondsPassedHandler(object clockInstance,
TimeEventArgs time);
//The clock publishes an event that others subscribe to
public event TwoSecondsPassedHandler TwoSecondsPassed;
public void Start()
{
while(true)
{
Thread.Sleep(2000);
//Raise event
TwoSecondsPassed(this, new TimeEventArgs(1));
}
}
}
public class TimeEventArgs : EventArgs
{
public TimeEventArgs(int second)
{
seconds += second;
instanceSeconds = seconds;
}
private static int seconds;
public int instanceSeconds;
}
public class MainClass
{
static void Main(string[] args)
{
Clock cl = new Clock();
// add some subscribers
cl.TwoSecondsPassed += new
Clock.TwoSecondsPassedHandler(Subscriber1);
cl.TwoSecondsPassed += new
Clock.TwoSecondsPassedHandler(Subscriber2);
cl.Start();
Console.ReadLine();
}
public static void Subscriber1(object clockInstance, TimeEventArgs time)
{
Console.WriteLine("Subscriber1:" + time.instanceSeconds);
}
public static void Subscriber2(object clockInstance, TimeEventArgs time)
{
Console.WriteLine("Subscriber2:" + time.instanceSeconds);
}
}
}
Ques 44. What is a value type and a reference type?
A reference type is known by a reference to a memory location on the heap.
A value type is directly stored in a memory location on the stack. A reference type is
essentially a pointer, dereferencing the pointer takes more time than directly accessing
the direct memory location of a value type.
Ques 45. Name 5 built in types.
Bool, char, int, byte, double
Most helpful rated by users:
- .NET Stands for?
- Name 10 C# keywords.
- What is public accessibility?
- What is protected accessibility?
- What is protected internal accessibility?