Friday, November 30, 2018

Method Hiding and Method Overriding


//Shadowing replaces the complete element of the parent.In simple word it can change a
//variable element of parent to a method or function

//Shadowing  is useful when get a requirement where the vocabulary will be kept
//same but the data type needs to be change.

//Overriding changes implementation while Shadowing replaces element with a

//complete new element only the interface vocabulary is same


//Method Hiding
namespace MethodHiding
{
    class A
    {
        public void PrintName()
        {
            Console.WriteLine("Print A");
        }
    }
    class B : A
    {
        public new void PrintName()
        {
            Console.WriteLine("Print B");
        }
    }
    class C : B
    {
        public new void PrintName()
        {
            Console.WriteLine("Print C");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            B objB = new B();
            objB.PrintName();//B

            C objC = new C();
            objC.PrintName();//C

            A objAB = new B();
            objAB.PrintName();//A

            B objCB = new C();
            objCB.PrintName();//B
        }
    }
}

//Method Overriding
namespace MethodOverriding
{
    class A
    {
        public virtual void PrintName()
        {
            Console.WriteLine("Print A");
        }
    }
    class B : A
    {
        public override void PrintName()
        {
            Console.WriteLine("Print B");
        }
    }
    class C : B
    {
        public override void PrintName()
        {
            Console.WriteLine("Print C");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            B objB = new B();
            objB.PrintName();//B

            C objC = new C();
            objC.PrintName();//C

            A objAB = new B();
            objAB.PrintName();//B


            B objCB = new C();
            objCB.PrintName();//C
        }
    }
}

Thursday, November 22, 2018

Pop vs Push & Shift vs Unshift

pop(): Remove an item from the end of an array
let cats = ['Bob', 'Willy', 'Mini'];
cats.pop(); // ['Bob', 'Willy']
pop() returns the removed item.

push(): Add items to the end of an array
let cats = ['Bob'];
cats.push('Willy'); // ['Bob', 'Willy']
cats.push('Puff', 'George'); // ['Bob', 'Willy', 'Puff', 'George']
push() returns the new array length.


shift(): Remove an item from the beginning of an array
let cats = ['Bob', 'Willy', 'Mini'];
cats.shift(); // ['Willy', 'Mini']
shift() returns the removed item.

unshift(): Add items to the beginning of an array
let cats = ['Bob'];
cats.unshift('Willy'); // ['Willy', 'Bob']
cats.unshift('Puff', 'George'); // ['Puff', 'George', 'Willy', 'Bob']
unshift() returns the new array length.

Splice vs Slice

  1. The splice() method returns the removed item(s) in an array and slice() method returns the selected element(s) in an array, as a new array object.
  2. The splice() method changes the original array and slice() method doesn’t change the original array.
  3. The splice() method can take n number of arguments and slice() method takes 2 arguments.
Splice with Example
Argument 1: Index, Required. An integer that specifies at what position to add /remove items, Use negative values to specify the position from the end of the array.
Argument 2: Optional. The number of items to be removed. If set to 0(zero), no items will be removed. And if not passed, all item(s) from provided index will be removed.
Argument 3…n: Optional. The new item(s) to be added to the array.
var array=[1,2,3,4,5];
console.log(array.splice(2));
// shows [3, 4, 5], returned removed item(s) as a new array object.
 
console.log(array);
// shows [1, 2], original array altered.
 
var array2=[6,7,8,9,0];
console.log(array2.splice(2,1));
// shows [8]
 
console.log(array2.splice(2,0));
//shows [] , as no item(s) removed.
 
console.log(array2);
// shows [6,7,9,0]

Slice with Example
Argument 1: Required. An integer that specifies where to start the selection (The first element has an index of 0). Use negative numbers to select from the end of an array.
Argument 2: Optional. An integer that specifies where to end the selection but does not include. If omitted, all elements from the start position and to the end of the array will be selected. Use negative numbers to select from the end of an array.
var array=[1,2,3,4,5]
console.log(array.slice(2));
// shows [3, 4, 5], returned selected element(s).
 
console.log(array.slice(-2));
// shows [4, 5], returned selected element(s).
console.log(array);
// shows [1, 2, 3, 4, 5], original array remains intact.
 
var array2=[6,7,8,9,0];
console.log(array2.slice(2,4));
// shows [8, 9]
 
console.log(array2.slice(-2,4));
// shows [9]
 
console.log(array2.slice(-3,-1));
// shows [8, 9]
 
console.log(array2);
// shows [6, 7, 8, 9, 0]




Thursday, October 25, 2018

LINQ Query VS LINQ Extension Method

LINQ Query

Restriction
from c in context.Courses

where c.Level == 1

select c;

Ordering
from c in context.Courses

where c.Level == 1

orderby c.Name, c.Price descending select c;
Projection
from c in context.Courses

select new { Course = c.Name, AuthorName = c.Author.Name };


Grouping
from c in context.Courses

group c by c.Level into g

select g;


from c in context.Courses

group c by c.Level into g

select new { Level = g.Key, Courses = g };

Inner Join
Use when there is no relationship between your entities and you need to link them based on a key.
from a in context.Authors

join c in context.Courses on a.Id equals c.AuthorId select new { Course = c.Name, Author = a.Name };

Group Join
Useful when you need to group objects by a property and count the number of objects in each group. In SQL we do this with LEFT JOIN, COUNT(*) and GROUP BY. In LINQ, we use group join.
from a in context.Authors

join c in context.Courses on a.Id equals c.AuthorId into g select new { Author = a.Name, Courses = c.Count() };

Cross Join
To get full combinations of all objects on the left and the ones on the right.
from a in context.Authors

from c in context.Courses

select new { Author = a.Name, Course = c.Name };

LINQ Extension Method

Restriction 
context.Courses.Where(c => c.Level == 1); 

Ordering 
context.Courses

.OrderBy(c => c.Name)      // or OrderByDescending

.ThenBy(c => c.Level); // or ThenByDescending



Projection

context.Courses.Select(c => new

{

CourseName = c.Name,

AuthorName = c.Author.Name       // no join required

});  
// flattens hierarchical lists

var tags = context.Courses.SelectMany(c => c.Tags); 


Grouping 

var groups = context.Courses.GroupBy(c => c.Level); 

Inner Join 
Use when there is no relationship between your entities and you need to link them based on a key.

var authors = context.Authors.Join(context.Courses,

a => a.Id,      // key on the left side

c => c.AuthorId,          // key on the right side,

(author, course) =>          // what to do once matched

new
{
AuthorName = author.Name,
CourseName = course.Name

}
); 


Group Join 
Useful when you need to group objects by a property and count the number of objects in each group. In SQL we do this with LEFT JOIN, COUNT(*) and GROUP BY. In LINQ, we use group join.

var authors = context.Authors.GroupJoin(context.Courses,

a => a.Id,      // key on the left side

c => c.AuthorId,          // key on the right side,

(author, courses) =>          // what to do once matched

new
{
AuthorName = author.Name,
Courses = courses

}
);


Cross Join 
To get full combinations of all objects on the left and the ones on the right. 
var combos = context.Authors.SelectMany(a => context.Courses, (author, course) => new {

AuthorName = author.Name,

CourseName = course.Name
});
  


Partitioning 
To get records in a given page. 
context.Courses.Skip(10).Take(10);

  
Element Operators  
//  throws an exception if no elements found context.Courses.First(); context.Courses.First(c => c.Level == 1);

//   returns null if no elements found context.Courses.FirstOrDefault();

//   not supported by SQL Server context.Courses.Last(); context.Courses.LastOrDefault();

context.Courses.Single(c => c.Id == 1);

context.Courses.SingleOrDefault(c => c.Id == 1);


Quantifying 

bool allInLevel1 = context.Courses.All(c => c.Level == 1); 
bool anyInLevel1 = context.Courses.Any(c => c.Level == 1); 


Aggregatin

int count = context.Courses.Count();

int count = context.Courses.Count(c => c.Level == 1);

 var max = context.Courses.Max(c => c.Price);

var min = context.Courses.Min(c => c.Price);

var avg = context.Courses.Average(c => c.Price);

var sum = context.Courses.Sum(c => c.Price);

Find the value from array when age is more than 30

 const data = [   { id: 1, name: 'Alice', age: 25 },   { id: 2, name: 'Bob', age: 30 },   { id: 3, name: 'Charlie', ...