Int d = Int64.Parse(obj) : This is an attempt to parse the null reference obj as a long value using the Int64.Parse method. ![]() The resulting value of d will be 0 (the default value for int), because the variable was not assigned any other value. Since obj is a null reference, the result of this cast will also be null. ![]() Int d = obj as int : This is a safe cast that attempts to convert obj to an int, but instead of throwing an exception if the cast fails, it returns null. This will result in a System.NullReferenceException at runtime, because you cannot cast a null reference to a value type like int. Int d = (int)obj : This is an explicit cast that attempts to convert the null reference obj to an int. Now, let's take a look at what happens in each of the scenarios you provided: In the following code, obj is declared as a null object reference of type object: object obj = null Q- object obj = null (C#) (With) - int d = (int)obj - int d = obj as int - int d = Int64.Parse(obj) - int d = Int64.TryParse(obj) - int d = obj is int What happend in all this cases ? It is important to understand the limitations of each type of casting, and to be mindful of potential data loss or type mismatches when converting values. We also demonstrate boxing by assigning an int to an object, and unboxing by casting the object back to an int.Ĭasting is a powerful tool in C#, but it should be used with care, as it can lead to unexpected results if not used properly. In the above example, we use implicit casting to convert an int to a double, and explicit casting to convert a double to an int. Int d = (int)obj // Casting obj back to an int Object obj = c // Boxing c into an object Here's an example of casting in C#: // Implicit Castingĭouble y = x // No cast operator required For example, you can unbox an int by casting the object to an int, like this: int i = (int)obj. Unboxing: This is the process of extracting the value from a boxed object and converting it back to a value type. For example, you can box an int by assigning it to an object, like this: object obj = 42. For example, you can explicitly cast a double to an int by using the cast operator, like this: (int)3.14.īoxing: This is the process of converting a value type to a reference type, by wrapping the value type in an object. Explicit casting requires the use of a cast operator, which is a set of parentheses containing the target type. For example, you can implicitly cast an int to a double, because there is no loss of precision.Įxplicit Casting: This is when you explicitly tell the compiler to convert a value from one type to another. Implicit Casting: This is when the compiler automatically converts a value from one type to another, if it can be done safely. There are several types of casting that you can use to convert values: ![]() Q- What is Casting and type of casting in C#? (With)Ĭasting is the process of converting a value from one data type to another in C#. Common-Interview-Questions-And-Answers (All Examples using C# language) General Topics:
0 Comments
Leave a Reply. |