Q31. Give real life example of abstract class usage in your project?
Q32. What is dictionary?
Q33. How to sort elements in dictionary by value?
Q34. What are delegates? How it is related to lambda expression.
Q35. What is event driven programming?
===================================================================================
Q31. Give real life example of abstract class usage in your project?
Answer:
In my project we have a abstract repository Generic class so that all the repositories will have basic functionality of repository
and they need not to implement again and again unless required.
public abstract class Repository<T>
{
//some simple methods, which will be called from all repository
public void InsertonSubmit(T entity)
{
//
}
//some virtual methods, which can be changed if required
public virtual void UpdateonSubmit(T entity)
{
//
}
//Some abstract method, which always depend on derived class but we want to enforce the //remembrance of writing logic
public abstract void ChangeSessionValues(T entity)
// child class will give definition.
}
===================================================================================
Q32. What is dictionary?
Answer:
public class Dictionary<TKey,TValue>
Dictionary<string, string> openWith = new Dictionary<string, string>();
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
Console.WriteLine(openWith["bmp"]);
===================================================================================
Q33. How to sort elements in dictionary by value?
Answer:
Dictionary is a collection thus linq and lambda can be applied on it.
var ans = openWith.orderby(key=>key.value
===================================================================================
Q34. What are delegates? how it is related to lambda expression
Answer:
Delegates are pointer to the function. Delegates are of two types 1. Singlecast and 2. Multicast.
Lambda expression - is an anonymous method that can be used to create delegates.
When you create delegates you can pass method name as a parameter to delegate object.
- using System;
- namespace DAL {
- public delegate void MyDelegate();
- class Program {
- public void CallerOne() {
- Console.WriteLine("This is Caller One function...");
- }
- static void Main() {
- //Create object of the class and give it an instance
- Program p = new Program();
- //Crete the object of delegate and note that you have to pass the method as parameter
- MyDelegate myDelegate = new MyDelegate(p.CallerOne);
- myDelegate();
- }
- }
- }
Multicast Delegate
- using System;
- namespace DAL {
- public delegate void MyDelegate();
- class Program {
- public void CallerOne() {
- Console.WriteLine("This is Caller One function...");
- }
- public void CallerTwo() {
- Console.WriteLine("This is Caller Two function...");
- }
- public static void CallerThree() {
- Console.WriteLine("This is Caller Three function...");
- }
- static void Main() {
- //Create object of the class and give it an instance
- Program p = new Program();
- //Crete the object of delegate and note that you have to pass the method as parameter. This is Single Cast
- MyDelegate myDelegate = new MyDelegate(p.CallerOne);
- //Crete the object of delegate and note that you have to pass the method as parameter. This is Single Cast
- MyDelegate myDelegate1 = new MyDelegate(p.CallerTwo);
- //Crete the object of delegate and note that you have to pass the method as parameter. This is Single Cast
- MyDelegate myDelegate2 = new MyDelegate(CallerThree);
- myDelegate();
- myDelegate1();
- myDelegate2();
- //This Idea is Called Multi Cast
- MyDelegate MyDelegate_MultiCast_Idea = myDelegate + myDelegate1 + myDelegate2;
- MyDelegate_MultiCast_Idea();
- }
- }
- }
Lambda Expression -- It reduces the reference to the method name part. as it become the part of delegate itself as an anonymous method.
- using System;
- namespace DAL {
- public delegate void MyDelegate();
- class Program {
- static void Main() {
- //Here '=>' thing work to implement Labda Expression. It can be further dicsussed but I will discuss this in future article.
- MyDelegate myDelegate = new MyDelegate(() => {
- Console.WriteLine("This is Caller One function...");
- });
- myDelegate();
- }
- }
- }
===================================================================================
Q35. What is event driven programming?
Answer:
As the name suggest this technique uses events as base for developing the software. We can have a software which is running without any interaction with events but usually these days software are developed with event interaction. events could be mouse click, uploading a video, visiting a new page etc.
===================================================================================
No comments:
Post a Comment