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);
}
}

23Sep/100

Named Parameters in C#4.0

In the last example, we saw that the following call was invalid:
Process( "foo", myArrayList ); // Invalid!

But if the boolean ignoreWS is optional, why can't we just omit it? Well, one reason is for readability and maintainability, but primarily because it can become impossible to know what parameter you are specifying. If you had two parameters of the same type, or if one of the parameters was "object" or some other base class or interface, the compiler would not know which parameter you are sending. Imagine a method with ten optional parameters and you give it a single ArrayList. Since an ArrayList is also an object, an IList, and an IEnumerable, it is impossible to determine how to use it. Yes, the compiler could just pick the first valid option for each parameter (or a more complex system could be used), but this would become impossible for people to maintain and would cause countless programming mistakes.

Named parameters provide the solution:
ArrayList myArrayList = new ArrayList();
Process( "foo", true ); // valid, moreData omitted
Process( "foo", true, myArrayList ); // valid
Process( "foo", moreData: myArrayList); // valid, ignoreWS omitted
Process( "foo", moreData: myArrayList, ignoreWS: false ); // valid, but silly

As long as a parameter has a default value, it can be omitted, and you can just supply the parameters you want via their name. Note in the second line above, the 'true' value for ignoreWS did not have to be named since it is the next logical parameter.

7Aug/100

What is the difference between overloading and overriding.

difference between overloading and overriding.

Overriding - Methods have the same signature as the parent class method.
Overloading - Methods have different parameters list or type or the return type.

Advantages and disadvantages of using multithreading

Advantages:

Simultaneous access to multiple applications
Reduced number of required servers
Improved performance and concurrency
Simplified coding of remote procedure calls and conversations

Disadvantages:

Code writing, debugging, managing concurrency, testing, porting existing code is difficult in multithreading and multicontexting.

Programmers need to remove static variables and replace any code that is not thread-safe to introduce threading into a previously non threaded application.