What is Action, Event and Delegate in simple?
A Delegate is a type which can help passing a function as a parameter. Events help two objects communicate with each other.
class A
{
public virtual int Add()
{
Console.WriteLine("A.Add()");
return 5 + 4;
}
}
class B : A
{
public sealed override int Add()
{
Console.WriteLine("B.Add()");
return 7 + 3;
}
}
class C : B
{
// throws a compilation error here
// 'C.Add()': cannot override inherited
// member 'B.Add()' because it is sealed
public override int Add()
{
Console.WriteLine("C.Add()");
return 99 + 1;
}
}
Can you add a ComplexType as a Key in a Dictionary<T, int>()?
Yes. Technically it is possible. As long as the object references that are passed to the keys of the dictionary are not equal (meaning they're new instances of the type T), a dictionary can accept them without any exception.
class A
{
public virtual int Add()
{
return 5 + 4;
}
}
Dictionary<A, int> dict = new Dictionary<A, int>();
for (int i = 0; i < 10; i++)
{
dict.Add(new A(), i);
}
foreach (var kv in dict)
{
Console.WriteLine($"{kv.Key} => {kv.Value}");
}
interface IDope {
int DoProp {get;set;}
int DoAdd(int a, int b);
}
Although both Decorator and Extension method try to extend the functionality of an existing component without having to modify it, the way they do is different.
The answer is No. The Dispose method is invoked by the runtime only when the class that implements the IDisposable interface is initialized with a static block setup.
class MyClass : IDisposable
{
public void Dispose()
{
Console.WriteLine("Calling MyClass.Dispose");
}
}
MyClass c = new MyClass();
using(MyClass uc = new MyClass()) {}
output:
Calling MyClass.Dispose //printed only once