Tuesday, January 29, 2019

All about constructors

Constructors

Every field(int, string, bool) inisintialized in the constructor. When we say every value types is defaulted to a value, that default value is provided in the constructor. When you do not give the constructor, .net gives a defaultc constructors and assign a default value.

Class Test
{
    int i; String j;
    Public test()
    {
  i=20; j=null;
     };//implicit constructor
}

Implicitly defined constructor are public by default.

Types of constructor
------------
1. Default (or parameterless constructor)
2. Parameterised Constructor
3. Copy Construtcor
4. Static constructor

Copy Constructor:
--------
  class CopyDemo
    {
        int x;
        public CopyDemo(int i)
        {
            x = i;
        }

        public CopyDemo(CopyDemo cd)  // this is copy constructor
        {
            x = cd.x;
        }
    }
---
copyDemo c1=new copyDemo(10);
copyDemo c2=new copyDemo(cd1);



Static Constructor
-------
 class Test
    {
        static Test() //implicitly defined only if we have static fields in class. No need to define it yourself.
        {

        }

        public Test()  // explicitly defined.
        {

        }

    }

If a class contains any static variables, then only implicit static constructor will be present. else we will have to define it manually.
VVImp-> Static constructor are implicitly called. They are called as soon as program execution begins. Non-Static constructors are called when a object has been declared on the class(via new object() call). Static constructor are called even if no object of that class if created.
Static constructors can not be overloaded or parameterised. This is because they are called implicitly, and hence, we never have a chance to manually call the static constructor.

---------------------


Why is constructor needed?
Ans-> To intiliaze variable of a class with same value even if we create multiple instances of the class.
A a = new A();
A b = new A();
A c = new A();
i.e. 3 different memoryallocations having same value

class B
{
   public int x;

  public B(int  a)
{
this.X=a;
}
}
Now if we create 3 instances, --todo


Static Vs Non- Static Constructor
---------------------------------
Static fields are intialized by static constructor
Non-Static fields are intialized by non-static constructor
Static constructor executes immediately only once when the execution of the class starts. It is the first block of code to be executed in a class.
Non Static constructor executes once and that too only after creating the instance of the class and it is called every single time a new object is created.
VVIMp:. Static constructor is called only once in a lifetime of a class.
Every single class except a static class constains an implicit non-static constructor if an explicit constructor is not defined.
Static constructor are implicitly defined in a non-static class if the class contains static fields.

Monday, January 28, 2019

Collections

Note: Collection are loosely coupled & Generics are strongly coupled.

Collections

Collections are of 4 types:
1. Index Based - Array, ArrayList, List
2. KeyVale Pair- HashTable, SortedList, Dictionary
3. Specialized - String Collection, CaseInsensitive Collection
4. Prioritised - Stack, Queue

Array
-----
3 things are not possible
1. Increased Size  2. Inserting in middle  3. Deleting or removing from middle
All these things are possible in Arraylist

ArrayList
---------
Intial default capacity is 0 (or to what we manually initialized)
Every Increment is by 4

HashTable
---------
While getting all the keys from the HT, it is not possible to get values in order since HT calculates the hash for every key(which is random), which is used in seraching algorithm.  Hastable key is object based.
Searching in HT is incredible fats beacuse of hashes.
HashList is way fatser as comapred to array and arraylist


Generics
-------
Collection came in c# 1.0 . Generics came in C# 2.0
For every class in Collection namespace, there is an equivalent class in  Colleciton.Generics namespace.

Collection<---> Generics
ArrayList <---> List
Hastable <---> Dictionary


IComparable Vs IComparer
------------------------
In my custom class, I can write a compare method if I want my class to perform sorting.
The method to implement is 'Compare(obj a)'.
If I have a class which is not my class, and I still want to sort the objects, I can use IComparer Interface.
IComparer has a method 'Compare(obj a, obj b)'.
Employee.sort()--->Implementing IComparable
Employee.sort(xyz)--> xmz is a class implementing IComparer.


IEnumerable
-----------
It is the parent interface for all collection classes.
Foreach in any class(List, Dictionary, ArraList, HT) works only because they have an implementation for foreach.

IEnumerable has a method 'GetEnumerator'  -- actual eg from list class --- public Enumerator GetEnumerator();
This 'GetEnumerator' is the sole reason we can loop through all the entries in a colleciton.
Return type of 'GetEnumerator' is Eumerator class which implements from IEnumerator interface.
IEnumerator interface has 3 methods


IEnumerable
   |--- IColleciton
  |--- IList  -- e.g. List, ArrayList
  |--- IDictionary -- e.g. Dictonary, Hashtable
this means...
IList implements ICollection implements IEnumerable
IDicstionary implements ICollection implements IEnumerable






Side Notes:
C# 1.0 - Collections
C# 2.0 - Generics
C# 3.0 - var  --- (checks/infers at compile time)
C# 4.0 - dynamic --- (checks/infers at run time)

Checked vs Unchecked Keyword

Checked vs Unchecked Keyword

.Equals vs ==

== does the reference comparison while .Equals does the value(or the content) comparison



Asp.net Core Continued

65 66 67 68 69 70 71 65 ASP.NET Core Identity It is a membership system Step 1 : Inherit from IdentityDbContext class instead of ...