Posts

Showing posts from August, 2022

Understanding Collection and Object Initializer

Image
  collection - we use it when we want to group objects of different types into one bucket. Remember that each item in a collection is an object. Example ArrayList adds all items in the form of objects so we were taking out values from the ArrayList instance then we must type from object type to its corresponding type   Here item is of object type, and we are type casting it to its corresponding type. collection Initializer - The group of object initializers are nothing but collection Initializer       Object Initialzier - It is used to initialize the data members or properties of a class.To avoid creating multiple constructor of a class, we go for Object Initialzier. Object   initializers creates parameterless constructor internally. The curly braces opening and closing is called Object   initializers block.  

Understanding LINQ

Image
why we should use  IEnumerable as return type  :  IEnumerable helps to iterate over a collection without knowing the actual datatype. It acts just like an ABSTRACTION - without knowing actual type Also may be IEnumerable is the base class of all the collections  classes (both generic and non-generic)  [ List, ArrayList,  Dictionary, HashTable  etc] LINQ (Language integrated query) - Using this we can write common code( common query syntax ) to fetch data from different data sources. Data sources means it can be from  Database like Sql server, oracle etc. XML files In memory objects like Lists, Arrays, collections The above is an advantage. Disadvantage are below It is quite difficult to write complex queries like we write in Sql server We might get performance issues in case if we do not write queries properly There is no cached plans like we do in Sql server Each query is a combination of three things. They are as follows: Initialization (to work with a particular data source) Conditi

Verbatim string

Image
 This is used to ignore the escape sequence characters and line breaks inside the string variables. Important thing to note here is that this will not ignore quote escape character (""). So to print quote as it is , you must use double quotes on that string. Ex

C# 6.0 Features

Image
  string interpolation - It is mainly introduced to remove string. Format. It is a new way of concatenating strings, manipulating strings and formatting strings Null conditional operator - this is used to avoid nullreference exception like object reference not set to an instance of an object. When we use this operator : it returns null as a value and wont throw any exception. Auto-property Initializer - This is used to set the properties values when they are declared at their place itself. Before this, we should use class constructor to initialize the properties to their default values. Dictionary/Index initializers - Before c#6.0, if we create Dictionary object then if you want to add any items into it, then we need to use Add() method. Now in c#6.0, we can do it similar to collection or object initializer way. Expression-body-function : This is nothing but similar to writing normal  code inside methods only this here we dont use "RETURN" keyword to return anything. Here we

Understanding ref and out + C#7.0 features

Image
ref and out - these are used in situations where some methods required to return multiple values from the method parameter list itself and not using 'return'  keyword, where method does not have 'return' keyword. Difference is -  1:ref variables must and should initialize or assigned the values before calling the method  2:out variables must be initialize or assigned the values inside the method. That is before leaving the method. In C#6.0, the out variables must and should be declared before calling the method. But in c#7.0,  the out variables can directly declared inside the method parameters c# 6.0 ------- int x , int y; obj.Math1(out x , out y); Here before using the variable it must be declared. c# 7.0 ------- obj.Math(out int x , out int y); Here we no need to declared it before using it in method parameters Tuple c# 6 ======= Tuple - these are used in situations where some methods required to return multiple values from the method, where method does have 'ret

Understanding Async and Await

Image
  To understand Async and Await, first we need to understand about 'Task'.  Task - This is used create thread but it will create thread from the thread pool and not from the computer operating system unlike normal Thread object in c# does. This is used to perform async operations 1. Task<TResult> - This return is used when the async method that returns a value. 2. Task - This return type is used when the async method does not return any value. 3. void - This return type is used for an event handler. We saw that concurrency refers to, in one way or another, doing several things at the same time. That concept of concurrency encompasses parallel programming and asynchronous programming. Parallel programming refers to the use of multiple threads simultaneously to solve a set of tasks. For this, we need processors with adequate abilities to perform several tasks at the same time. In general, we use parallel programming to gain speed. Asynchronous programming refers to the ef

Understanding Delegates

Image
Delegates are of two types  Normal delegates  Generic delegates 1:Normal delegates - These delegates again will fall into three ways of writing it or using it Simple delegates Anonymous delegates Lambda delegates Simple delegates - these are delegates who are called in a normal way.  Declare delegate Define the method which will match the declared delegate Inside the Main method -  - Create object of delegate with method name in its constructor - Invoke delegate : Pass parameters if any inside the object of delegate similar to method. - Print the values if needed  Anonymous delegates : These are very similar to normal delegates. The only difference is that the method definition is given directly inside the delegate's constructor with the DELEGATE keyword. Lambda delegate - This is also similar to simple delegates. The difference is that here we are not creating a delegate object by using the “new” keyword instead we use the object reference of delegate and use lambda operator =&g

Understanding Flow of MVC

 First request will come to controller, controller will get data from Model( if there is a code to do so), after getting data from model, this data will be sent to view by the controller. Model will never talk to view, controller will always act as bridge between these two guys

Understanding Properties

  Now a days properties has become rule to declare in every class. I don’t think it should be used in every  class that we declare. It all depends on what is the scenario in our project whether we need properties or declare them? Well I guess it should be used in all classes because as we all know that variables inside class are private by default and cannot be accessed outside it’s class. In order to access private variables of the class then we have only two options Declare with public - not good idea as anything can be set into it and by anyone. Declare with properties Advantage by declaring properties is that, either we can allow only to read access with “GET” or we can give SET access by checking some conditions and then only set those values. I think if a property is declared with get and set without special conditions inside get and set then it is better to declare them with public we can also declare property which is of type ENUM. we can also define  property which is of type

Understanding Indexer

 Indexer in c# is same as Properties. They contain both get and set methods just like Properties. Question arises if they are similar as Properties then why do we indexer?well, we all know that by default class variables are private and cannot be accessed outside of its class. if you want to maintain private-ness of the variables inside the class and want to access those variables outside of class then we have only two options Declare it as public - this is not safe Declare it as properties - this is safe choice Now if  you don’t want to declare the above two points but still want to access class variables then the other is declaring indexer inside the class. So if you declare indexer inside any class then that class will become or act like VIRTUAL ARRAY.

Understanding Dependency Injection

Image
Before getting into Dependency injection, we need to understand about IOC and DIP. Both IOC and DIP are principles or you can think it as rules that are set to achieve loosely coupled design between two classes. These two classes can be thought of as class A called as "PARENT" and class B called as "CHILD". It can be thought of as two project or two software components. These two classes should not depend of each other. I mean to say "PARENT" class should not depend on "CHILD" class to achieve something. Here depend means : inside "PARENT" class we should not create the object of "CHILD" class. Ex: Here in the above image you can see the "PARENT" class is creating "CHILD" class object to achieve something. Hence we can say that "PARENT" class is "DEPENDENT" on "CHILD" class or we can also that "CHILD" class is a dependency class of "PARENT" class. So to make