If at first you don't succeed, call it version 1.0

Contact Me

Sarvesh Kushwaha
Email : sarveshkushwaha@outlook.com

Total Pageviews

Powered by Blogger.

Showing posts with label C# Fundamentals. Show all posts
Showing posts with label C# Fundamentals. Show all posts

Sunday, 12 February 2017

10 Essentials C# keywords every .Net developer should know before attending an Interview #AspNetInterviewQuestionsSeries


1. Var and/vs Dynamic

With Var keyword properties / Methods /Operators etc are resolve at compile time.
Var keyword is static typed and type cannot be change,Once you have assigned the type to var.

var i = "abc";
i = 1 ; //you cannot assign it integer value once it has been marked as string


With Dynamic keyword properties / Methods /Operators etc are resolve at runtime.
type can be change once it is already assigned.

dynamic i = "abc";
i = 1 ; //you can change the type in dynamic
Console.WriteLine(i.anything); //error will be given at runtime

2. Const and ReadOnly
  1. Constants are evaluated at the compile-time, while the read-only variables are evaluated at the runtime. 
  2. Constants support only value-type variables, while read-only variables can hold reference type variables. 
  3. Constants should be used when the value is not changing during the runtime, and read-only variables are used mostly when their actual value is unknown before the runtime.

static readonly string readonlystring;   //you can levae it blank and can assign value at runtime,assign object to it 
 const string constring ="abc"; // you have to assign some value to const while declaring,you can assign ref type to const

3. Checked and Unchecked keyword
Checked keyword is to check integral arithmetic overflow.
What will happen if you are assigning the greater value to your int than its maximum value?
you will get the wrong result,to check above condition and throw airtmetic overflow use Checked keyword either in block () or in expression {} manner.



//checked block
int checkedk = int.MaxValue;
  Console.WriteLine(checked(checkedk+ 20));
  
  //checked expression
   checked
   {
    int i = 10;
     int i3 = 2147483647 + i;
                Console.WriteLine(i3);
   }

unchecked keyword is just opposite of that it will not throw any exception. by default int ,byte are unchecked.

4. AS and IS keyword
Both keywords are used for safe cast typing with lill diff in their working.

IS : 
checks whether the type of an given object is compatible with the new object type and returns Boolean type true or false.

AS:
checks whether the type of an given object is compatible with the new object type and returns object if it is compatible or null if it isn't.

person p = new person();
     Men men = new Men();
  
  Men m2 =  p as Men;
  Console.WriteLine(p is Men);
  Console.WriteLine(men is person);
  if(m2 !=null)
  {
   Console.WriteLine("m is person type");
  }else
  {
     Console.WriteLine("m is not person type");
  }


5. Volatile and Lock keyword
volatile indicates that field can be modified by multiple thread. volatile keyword can be applied only to fields of class/struct.

Lock keyword ensures that if one thread is accessed any object in lock block that will not be accessible by another thread, it will have to wait until that thread release the accessed object.



Sunday, 29 January 2017

What are Func, Predicate, Action delegates with Examples ? #AspNetInterviewQuestionsSeries


To learn Delegate you guys can refer my previous article. In this article we will learn about what are Func, Predicate and Action Delegate.
Keeping the power of delegates in the mind .Net Framework has introduced the predefined delegates which can be useful for a developer. .Net Framework has also used these predefined delegates at many places e.g LINQ.

Action Delegate: As its name is ACTION this delegate ensures to performs actions and does not return anything. Action Delegate can take upto 16 parameters and returns nothing as they are void delegates and their primary task to perform some action. You can create Action in C# :

// define action 
Action<string> myAction;
// F12 definition will be of your action, it can go upto 16 parameters
public delegate void Action<in T>(T obj);
   


Example : they are most commonly getting used in List<T>.ForEach  like functions.

Friday, 11 April 2014

Thursday, 10 April 2014

Sunday, 6 April 2014

CLR in .Net


Common Language Runtime : 

As its names depicts, its an execution environment for .Net applications (.Net supported languages). CLR job is managing the application while it executes.CLR manages the execution of programs written in supported language of .NET framework like (C#, Visual Basic, F#, IronPython, IronRuby, Boo, etc.).