C# Interview Questions @2023
1. What is Datatype conversion ? What are different way of type conversion ?
- Datatype conversion is nothing but converting one data type to another data type for example converting a int value to a float value or vice versa.
- There are 2 different types of Datatype conversion
a) Implicit Conversion – Compiler automatically does a conversion.
b) Explicit Conversion – When compiler fails for automatic conversion we have to do conversion explicitly.
- Compiler does the conversion automatically in 2 cases
a) When there is no loss of information
b) When there are no chances of runtime exception
- Converting from a smaller datatype to bigger datatype for example int to float in this case there is no loss of information or there is no chance of any exception so compiler will automatically do a conversion which is implicit type conversion. But if we reverse this case let’s say converting a float value to int then there is definitely a loss of information means int variable will not be able to hold fractional part and also there is a chance of Overflow Exception. So compiler will not perform implicit conversion and we have to use explicit cast here.
2. What are different ways of explicit type conversions?
- There are different ways to achieve explicit type conversion –
a) Type casting.
Double a = 25;
int b = (int) a;
b) Using of Convert class.
float a = 95;
int b = Convert.ToInt32(a);
We can use Parse and TryParse methods when we need to convert a string to other
types like int,bool, float etc.
3. What is difference between Parse and TryParse methods ?
If the number is in a string format you have 2 options
a) Parse()
-This method takes string value as parameter and tries to convert to corresponding numeric type.
- Parse() method throws an exception if it cannot parse the value.
b) TryParse()
- The method takes a string value as an input parameter, the resultant conversion value as out parameter, a return value of True for successful conversion and False for unsuccessful conversion.
- TryParse() returns a bool indicating whether it succeeded or failed.
- We can use Parse() if we are sure the value will be valid, otherwise use TryParse()
4. Difference between Value type and Reference types?
- Value types gets stored on stack and Reference types gets stored on heap and its reference gets stored on Stack.
- By default, Value types cannot hold null values but it can be achieved by using nullable types. Reference types can contain null values.
- Value types gets destroyed or released from the memory when they go out of scope whereas for reference types requires garbage collector to free memory.
- enum, struct is of value type.
- Class, string, object, array is of Reference type.
6. What is difference between var and object type and dynamic keyword?
a) Var -
- It is compile time variable and does not require boxing and unboxing.
- Since Var is a compile time feature, all type checking is done at compile time only.
- Once Var has been initialized, you can't change type stored in it.
- var a= 17; // after this line a has become of integer type
- a= a+ 10; // No error
- a= "hello"; // Compile time error as a is an integer type
b) Object -
- Each object in C# is derived from object type, either directly or indirectly.
- It is compile time variable and require boxing and unboxing for conversion and it makes it slow.
- You can change value type to reference type and vice versa.
- object a= 15;
- a = a+ 10; // Compile time error
- a= "hello"; // No error, Boxing happens here
c) Dynamic -
- It is run time variable and not require boxing and unboxing.
- You can assign and value to dynamic and also can change value type stored in same.
- All errors on dynamic can be discovered at run time only.
- We an also say that dynamic is a run time object which can hold any type of data.
- dynamic a= 10;
- a= a+ 10; // No error
- a= "hello"; // No error, neither compile time nor run time
7. Ternary operator in C#?
- It is shortcut to write a conditional statement.
- The conditional operator (?:) returns one of two values depending on the value of a Boolean expression.
condition? first expression : second expression;
8. What is break, continue, return keywords?
a) Break
- The break statement terminates the closest enclosing loop or switch statement in which it appears.
b) Continue
- The continue statement passes control to the next iteration of the enclosing while, do, for, or foreach statement in which it appears.
c) Return
- The return statement terminates execution of the method in which it appears and returns control to the calling method. If the method is a void type, the return statement can be omitted. But we can write return in a method which has void return type if your intention to pass control back to calling method. - If the return statement is inside a try block and there is finally block exists, then finally will be executed before control returns to the calling method.
Comments
Post a Comment