Search This Blog

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

Friday, March 4, 2016

Difference between ? and ?? operators in C#

Difference between ? and ??
            The conditional operator (?:) returns one of two values depending on the value of a       Boolean expression. Following is the syntax for the conditional operator.
      condition ? first_expression : second_expression;    
              // ?: conditional operator.
            int input = Convert.ToInt32(Console.ReadLine());           
            string classify = (input > 0) ? "positive" : "negative";

       The ?? operator is called the null-coalescing operator. It returns the left-hand operand if          the operand is not null; otherwise it returns the right hand operand.

            int? x = null;
            // Set y to the value of x if x is NOT null; otherwise,
            // if x = null, set y to -1.
            int y = x ?? -1;

Difference between as and is in C#


as
is
Is Operator is used to check the Compatibility of an Object with a given Type and it returns the result as a Boolean (True or false).
As Operator is used for Casting of Object to a given Type or a Class.
Ex:
if (someObject is StringBuilder) ...
Ex:
object x = 5;
// int y = x as int; // not allowed becoz of int : value type
int? y = x as int?; // allowed becoz of nullable type

Ex.         
Student s = obj as Student;                   
is equivalent to:
Student s = obj is Student ? (Student)obj : (Student)null;


as operator should be used with Reference Type or nullable type

Wednesday, November 13, 2013

Difference between Object, Dynamic and Var in c#

Object
Dynamic
Var
Can able to store any kind of value, because object is the base class of all type in .net framework.
Can able to store any type of the variable, similar to old VB language variable.
Can able to store any type of value but it require to initialize at the time of declaration.
Compiler has little information about the type
Compiler doesn't have any information about the this type of variable.
It's compiler safe i.e compiler has all information about the stored value, so that it doesn't cause any issue at run-time.
Object type can be passed as function argument and function also can return object type
Dynamic type can be passed as function argument and function also can return object type
Var type can not be passed as function argument and function can not return object type. This type of variable can work in the scope where it defined.
Require to cast object variable to original type before using it. So this assigning to object type and converting to original type called as Boxing and Un-Boxing for value type and for the reference type its casting of types. It's actually increasing the overhead when we do this both operation.
Allows to perform operation of given type once it get cast any user defined or primitive data type.
Casting is not require but you need to know the property and methods related to stored type
No need to cast because compiler has all information to perform operation.
Cause the problem at run time if the stored value is not get converted to underlying data type.
Cause problem if the wrong method or property accessed because all the information about stored value is get resolve only at run time
Doesn't cause problem because compiler has all info about stored value.
Useful when doesn't have more information about the data type.
Useful when coding using reflection or dynamic language support or with the COM objects, because we require to write less amount of code.
Useful when getting result out of the linq queries. In 3.5 framework it introduce to support linq feature.

Popular Posts