C#

What is the base class of all other types?

The type object is the ultimate base type of all other types.

What is unifiled type system? Twist: What is boxing and unboxing? what is the difference between boxing and unboxing?

In C#, all the types derive from object, including value types, usually call as primitive types. calls the object-defined ToString method on an integer literal, resulting in the output “3”.

using System;
class Test
{
	static void Main() {
		Console.WriteLine(3.ToString());
	}
}

Boxing an Unboxing example

class Test
{
	static void Main() {
		int i = 123;
		object o = i;		// boxing
		int j = (int) o;	// unboxing
	}
}

An int value can be converted to object and back again to int. This example shows both boxing and unboxing. 

Boxing, When a variable of a value type needs to be converted to a reference type, an object box is allocated to hold the value, and the value is copied into the box. 

Unboxing is just the opposite. When an object box is cast back to its original value type, the value is copied out of the box and into the appropriate storage location.

Another good example is Stack class, this class has two methods Push and Pop with argument as object type, hence user can push any type of values.

How many parameter types in c# and define it?

There are four kind of parameters

  • value
  • reference
  • output
  • parameter arrays

A value parameter is used for “in” parameter passing, in which the value of an argument is passed into a method, and modifications of the parameter do not impact the original argument.

A reference parameter is used for “by reference” parameter passing, in which the parameter acts as an alias for a caller-provided argument. A reference parameter does not itself define a variable, but rather refers to the variable of the corresponding argument. Modifications of a reference parameter impact the corresponding argument.

The ref keyword must be used in both the declaration of the formal parameter and in uses of it. This will help developer to understand that the value of the argument could change as a result of the call.

An output parameter is similar to a reference parameter, except that the initial value of the caller-provided argument is unimportant. An output parameter is declared with an out modifier.

For value, reference, and output parameters, there is a one-to-one correspondence between caller-provided arguments and the parameters used to represent them. A parameter array enables a many-to-one relationship: many arguments can be represented by a single parameter array. In other words, parameter arrays enable variable length argument lists. A parameter array is declared with a params modifier. e.g. static void F(params int[] args) {...}

What is difference between constants and read-only?

Constants
1.Constant variables are declared and initialized at compile time.
2.The value cannot be changed afterwards.
3.You will get compile time error, Left hand side of assignment my be variable, property or indexer.

Read only
1.Read only is used only when we want to assign the value at run time.
2.Value can be assigned during variable initializer or can be changed / overriden in constructor.
3.Read only field cannot be assigned to except constructor or initializer;

What are sealed classes in C#?

When we define class and no intention to inherit from any other class and they can never be used as a base class.
The sealed modifier is used to prevent derivation from a class. We encounter compile-time errors if a sealed class is specified as the base class of another class.
A sealed class cannot also be an abstract class.
.Net framework cliams that, some run-time optimizations can make calling sealed class members slightly faster.

Can we declare abstract method in Non-Abstract class?

No.
  
public  class Animal
{
	public abstract void Run();
}
abstract method defined in non-abstract class.

What is virtual method?

It is special method with defined certain logic or functionality to perform in abstract or non-abstract (normal) class. When we inheri this class, the special method which defined as virtual, which can be override or hides. in order to hide parent method, add new keyword

Static class can have constructor? Similar one: Static class can have non-static method?

No. Static class cannot have constructor because we cannot create instnces.
No.

Non-Static class can have static methods?

Yes, and accessd throguh class name.
Example
		  
internal class Department
{
	public int DeptId { get; set; }
	public string DeptName { get; set; }


	public static string Print(string name)
	{
		return name + myenum;
	}
	//Static method print, accessble through Class name 'Department'
	Department.Print();
}

Events and Delegates?

An event is a member that enables an object or class to provide notifications. A class defines an event by providing an event declaration (which resembles a field declaration, though with an added event keyword) and an optional set of event accessors. The type of this declaration must be a delegate type.
Delegates enable scenarios that some other languages have addressed with function pointers. However, unlike function pointers, delegates are object-oriented and type-safe.
A delegate declaration defines a class that is derived from the class System.Delegate. A delegate instance encapsulates an invocation list, which is a list of one or more methods, each of which is referred to as a callable entity.

public delegate void EventHandler(object sender, System.EventArgs e);
public class Button 
{
	public event EventHandler Click;
	public void Reset() {
		Click = null;
	}
}
the Button class defines a Click event of type EventHandler. Inside the Button class, the Click member is exactly like a private field of type EventHandler. However, outside the Button class, the Click member can only be used on the left-hand side of the += and -= operators. The += operator adds a handler for the event, and the -= operator removes a handler for the event. The example
using System;
public class Form1 
{
	public Form1() {
		// Add Button1_Click as an event handler for Button1’s Click event
		Button1.Click += new EventHandler(Button1_Click);
	}
	Button Button1 = new Button();
	void Button1_Click(object sender, EventArgs e) {
		Console.WriteLine("Button1 was clicked!");
	}
	public void Disconnect() {
		Button1.Click -= new EventHandler(Button1_Click);
	}
}
shows a Form1 class that adds Button1_Click as an event handler for Button1’s Click event. In the Disconnect method, that event handler is removed.
There are three steps in defining and using delegates: declaration, instantiation, and invocation. Delegates are declared using delegate declaration syntax. The example delegate void SimpleDelegate(); declares a delegate named SimpleDelegate that takes no arguments and returns no result. The example
class Test
{
	static void F() {
		System.Console.WriteLine("Test.F");
	}
	static void Main() {
		SimpleDelegate d = new SimpleDelegate(F);
		d();
	}
}
Creates a SimpleDelegate instance and then immediately calls it. There is not much point in instantiating a delegate for a method and then immediately calling that method via the delegate, as it would be simpler to call the method directly. Delegates really show their usefulness when their anonymity is used. The example
void MultiCall(SimpleDelegate d, int count) {
	for (int i = 0; i < count; i++) {
		d();
	}
}

shows a MultiCall method that repeatedly calls a SimpleDelegate. The MultiCall method doesn’t know or care about the type of the target method for the SimpleDelegate, what accessibility that method has, or whether or not that method is static. All that matters is that the target method is compatible (§15.1) with SimpleDelegate.

Assemblies and Namespaces

Namespaces and assemblies enable this component-based system. Namespaces provide a logical organizational system. Namespaces are used both as an “internal” organization system for a program, and as an “external” organization system—a way of presenting program elements that are exposed to other programs.
Assemblies are used for physical packaging and deployment. An assembly may contain types, the executable code used to implement these types, and references to other assemblies. There are two main kinds of assemblies: applications and libraries. Applications have a main entry point and usually have a file extension of .exe; libraries do not have a main entry point, and usually have a file extension of .dll.

Creating and using generics?

Generics provide a facility for creating types that have type parameters. The example below declares a generic Stack class with a type parameter T. The type parameter is specified in < and > delimiters after the class name. Rather than forcing conversions to and from object, instances of Stack accept the type for which they are created and store data of that type without conversion. The type parameter T acts as a placeholder until an actual type is specified at use. Note that T is used as the element type for the internal items array, the type for the parameter to the Push method, and the return type for the Pop method:

public class Stack
{
	T[] items;
	int count;
	public void Push(T item) {...}
	public T Pop() {...}
}
When the generic class Stack is used, the actual type to substitute for T is specified. In the following example, int is given as the type argument for T:
Stack stack = new Stack();
stack.Push(3);
int x = stack.Pop();

Anonymous methods?

Event handlers and other callbacks are often invoked exclusively through delegates and never directly. Even so, it has thus far been necessary to place the code of event handlers and callbacks in distinct methods to which delegates are explictly created. In contrast, anonymous methods allow the code associated with a delegate to be written “in-line” where the delegate is used, conveniently tying the code directly to the delegate instance. Besides this convenience, anonymous methods have shared access to the local state of the containing function member. To achieve the same state sharing using named methods requires “lifting” local variables into fields in instances of manually authored helper classes.
The following example shows a simple input form that contains a list box, a text box, and a button. When the button is clicked, an item containing the text in the text box is added to the list box.

class InputForm: Form
{
	ListBox listBox;
	TextBox textBox;
	Button addButton;
	public MyForm() {
		listBox = new ListBox(...);
		textBox = new TextBox(...);
		addButton = new Button(...);
		addButton.Click += new EventHandler(AddClick);
	}
	void AddClick(object sender, EventArgs e) {
		listBox.Items.Add(textBox.Text);
	}
}