Interview Tips Interview Tips, Interview Questions and Answers

27Nov/100

Using Delegate Parameters with Anonymous Methods

Many event handlers need to use the parameters of the delegate they are based on. The previous example didn't use those parameters, so it was more convenient to not declare them, which C# allows. Listing 21-2 shows you how to use parameters if you need to.
// Using Parameters with Anonymous Methods

using System;
using System.Windows.Forms;

public partial class Form1 : Form
{
public Form1()
{
Button btnHello = new Button();
btnHello.Text = "Hello";

btnHello.Click +=
delegate
{
MessageBox.Show("Hello");
};

Button btnGoodBye = new Button();
btnGoodBye.Text = "Goodbye";
btnGoodBye.Left = btnHello.Width + 5;

btnGoodBye.Click +=
delegate(object sender, EventArgs e)
{
string message = (sender as Button).Text;
MessageBox.Show(message);
};

Controls.Add(btnHello);
Controls.Add(btnGoodBye);
}
}

9Jun/100

How does multi-threading work in .NET?

There are two main ways of multi-threading which .NET encourages:

To start your own threads with ThreadStart delegates, you should create a new thread "manually" for long-running tasks.

Using the ThreadPool class either directly (using ThreadPool.QueueUserWorkItem) or indirectly using asynchronous methods (such as Stream.BeginRead, or calling BeginInvoke on any delegate).

Use the thread pool only for brief jobs.

Benefits of using the Thread Pool.

Benefits of using the Thread Pool.

The benefits of using a Thread Pool are:

Thread creation and destruction overhead is negated,
Better performance and better system stability.
It is better to use a thread pool in cases where the number of threads is very large.

25Feb/100

C# interview questions with answers

How can you check a nullable variable is assigned?
using hasvalue method

what is the syntax of a nullable variable?
Nullable myval=null;

I would like to check a variable as assigned or not. How can we do that?
Declare the variable as Nullable

What are the different compiler generated methods when a .NET delegate is compiled?
Compilation of a .NET delegate results in a sealed class with three compiler generated methods whose parameters and return values are dependent on the delegate declaration. The methods are,
Invoke()
BeginInvoke() and
EndInvoke().

When TimerCallback delegate is used?
Many application have the need to call a specific method during regular intervals. For such situations we can use the System.Threading.Timer type in conjunction with a related delegate named TimerCallback.

Which enumeration defines different PenCap in C#?
LineCap enumeration which defines different pen cap styles as follows,

public enum LineCap
{
Flat, Square, Round, Triangle, NoAncher, SquareAnchor,
RoundAnchor, DiamondAnchor, ArrowAnchor, AnchorMask, Custom
}

What is the use of param keyword in C#?
In C# param parameter allows us to create a method that may be sent to a set of identically typed arguments as a single logical parameter.