Tuesday 28 January 2020

Constructor execution hierarchy in C#

Class C1
{
  public C1()
   {
     Console.WriteLine("Normal C1");
   }
  static C1()
   {
     Console.WriteLine("Static C1");
   }
}
--------------------------------
Class C2: C1
{
  public C2()
   {
    Console.WriteLine("Normal C2");
   }
  static C2()
   {
     Console.WriteLine("Static C2");
   }
}

/////////////////////////////
Output:

Static C2
Static C1
Normal C1
Normal C2

Tuesday 21 January 2020

prototype in C#

Prototype in C# design pattern

Step1 : Create Interface
public interface IEmployee
    {
        IEmployee Clone();
        string GetDetails();
    }

Step2: Create Class and inherit interface
    public class Developer : IEmployee
    { 
        public int Id { get; set; }
        public string Name { get; set; }      
        public string PreferredLanguage { get; set; }

       // Clone object
        public IEmployee Clone()
        {           
            return (IEmployee)MemberwiseClone();
        }
        public string GetDetails()
        {
            return string.Format("{0} - {1} - {2}", Name, Id, PreferredLanguage);
        }
    }
 
  // Create another class and inherit Interface
    public class Tester : IEmployee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string PreferredLanguage { get; set; }
        public string Address { get; set; }
        public IEmployee Clone()
        {          
            return (IEmployee)MemberwiseClone();
        }
        public string GetDetails()
        {
            return string.Format("{0} - {1} - {2} - {3}", Name, Id, PreferredLanguage, Address);
        }
    }

// Implementation place
 public class PrototypeTest
    {
        public static void Test()
        {
            Developer dev = new Developer()
            {
                Name ="Dev1",
                Id =1,
                PreferredLanguage = "en-us"
            };
            Developer devCopy = (Developer)dev.Clone();
            devCopy.Id = 2;
            devCopy.Name = "Dev2";
            Console.WriteLine(dev.GetDetails());
            Console.WriteLine(devCopy.GetDetails());
        }
    }

// Result

Dev1 - 1 - en-us
Dev2 - 2 - en-us

Monday 20 January 2020

async await in C#

The async await comes in C# 5.0. This keywords is used in same function.
Suppose you have login task taht takes more time to response, it will stop the UI for this time.
So in this case you need to write asynchronous code.
The await keyword is used to stop the current method execution untill task is completed/ return result.
we can not use await if we not apply async keyword in function.

groupby multiple colunm

Step1: Suppose you have classes:

 public class Teacher
    {
        public string School { get; set; }
        public string Friend { get; set; }
        public string Book { get; set; }
        public List<Student> Children { get; set; }
    }

    public class Student
    {
        public string School { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string Friend { get; set; }
        public string Mother { get; set; }
        public string Book { get; set; }
    }

Step2: In step1 you have created class, now on your controller action method you can set some mock data as example given below:  List<Student> student = GetList();

Step3: after that we have apply query group by:-
 public class TestController : ControllerBase
    {

        [HttpGet]
        [Route("groupby")]
        public async Task<IActionResult> TestGroupBy()
        {
            List<Student> student = GetList();
            var result =
            from c in student
            group c by new { c.School, c.Friend, c.Book, } into gcs
            select new Teacher()
            {
                School = gcs.Key.School,
                Friend = gcs.Key.Friend,
                Book = gcs.Key.Book,
                Children = gcs.ToList(),
            };
            return Ok(result);
        }

        private static List<Student> GetList()
        {
            return new List<Student>()
                {
                    new Student()
                        {School = "S1", Book = "Book1", Friend = "Hamid", Name = "Stev"},
                    new Student()
                        {School = "S2", Book = "Book1", Friend = "Hamid", Name = "Bill"},
                    new Student()
                        {School = "S3", Book = "Book1", Friend = "Smith", Name = "Root"},
                    new Student()
                        {School = "S2", Book = "Book1", Friend = "Finch", Name = "Root"},
                };
        }
    } Ok(result);
        }

Step4: Result:
[
  {
    "school": "S1",
    "friend": "Hamid",
    "book": "Book1",
    "children": [
      {
        "school": "S1",
        "name": "Stev",
        "address": null,
        "friend": "Hamid",
        "mother": null,
        "book": "Book1"
      },
      {
        "school": "S1",
        "name": "Stev",
        "address": null,
        "friend": "Hamid",
        "mother": null,
        "book": "Book1"
      }
    ]
  },
  {
    "school": "S2",
    "friend": "Smith",
    "book": "Book1",
    "children": [
      {
        "school": "S2",
        "name": "Root",
        "address": null,
        "friend": "Smith",
        "mother": null,
        "book": "Book1"
      },
      {
        "school": "S2",
        "name": "Root",
        "address": null,
        "friend": "Smith",
        "mother": null,
        "book": "Book1"
      }
    ]
  }
]

Thursday 16 January 2020

filter collection from multiple values

Suppose we have a list MyList
Then we can take in enumerable like below

IEnumerable<TestObject> query = MyList;
if (obj1 != null) {
    query = query.Where(x => x.Id == obj1.ID);
}
if (obj2= null) {
    query = query.Where(x => x.Number.Contains(obj2.Number));
}
if (obje3 != null) {
    query = query.Where(x => x.Date == obj3.Date);
}
......
you can use many more
Finally we can convert in list, It basically reduce the complexcity

MYListView = query.ToList();