Advertisement

Responsive Advertisement

VI- Null Coalescing Operator (??) , VII-Cast Operator "( )"

 

VI- Null Coalescing Operator (??)

The null coalescing operator is used with the nullable value types and reference types. It is used for converting an operand to the type of another nullable (or not) value type operand, where an implicit conversion is possible.


If the value of the first operand is null, then the operator returns the value of the second operand, otherwise, it returns the value of the first operand.

VII-Cast Operator "( )"

Cast, in the context of C#, is a method by which a value is converted from one data type to another. Cast is an explicit conversion by which the compiler is informed about the conversion and the resulting possibility of data loss.


Cast is typically used when the explicit conversion should be aware to the users who are performing the cast operation. It helps in the conversions where information might be lost or that might not succeed for other reasons. Cast operations can be performed for numeric conversions in which the destination type is of lesser precision or a smaller range. It is also used for conversion from base class instance to derived class.


Due to the inherent feature of variables in C# language being statically typed at compile time, variables declared once in code cannot be declared again and store values of another type, unless that type is convertible to a variable‘s type. Cast helps in copying a value of a particular type into a variable or parameter of a method which is of different type.


Post a Comment

0 Comments