# InvalidCastException

***

**1. Convert from a derived type to a base type:**

```csharp
class BaseClass { }
class DerivedClass : BaseClass { }

DerivedClass derived = new DerivedClass();
BaseClass baseClass = (BaseClass)derived;
```

**2. Convert from an object to a specific type:**

```csharp
object obj = "Hello";
string str = (string)obj;
```

**3. Convert from a string to an enumeration:**

```csharp
string enumValue = "Value1";
MyEnum myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), enumValue);
```

**4. Convert from a string to a nullable type:**

```csharp
string nullableValue = "123";
int? nullableInt = (int?)Convert.ChangeType(nullableValue, typeof(int?));
```

**5. Convert from a value type to a reference type:**

```csharp
int intValue = 123;
object obj = (object)intValue;
```

**6. Convert from a reference type to a value type:**

```csharp
object obj = 123;
int intValue = (int)obj;
```

**7. Convert from a boxed value type to an unboxed value type:**

```csharp
object obj = 123;
int intValue = (int)(int)obj;
```

**8. Convert from an interface to a concrete class:**

```csharp
interface IMyInterface { }
class MyConcreteClass : IMyInterface { }

IMyInterface myInterface = new MyConcreteClass();
MyConcreteClass myConcreteClass = (MyConcreteClass)myInterface;
```

**9. Convert from a generic type parameter to a specific type:**

```csharp
class MyGenericClass<T> { }

MyGenericClass<int> myGenericClass = new MyGenericClass<int>();
MyGenericClass<string> mySpecificClass = (MyGenericClass<string>)myGenericClass;
```

**10. Convert from a nullable type to a non-nullable type:**

```csharp
int? nullableInt = 123;
int nonNullableInt = (int)nullableInt;
```

**11. Convert from a DateTime to a string:**

```csharp
DateTime dateTime = DateTime.Now;
string dateString = dateTime.ToString();
```

**12. Convert from a string to a DateTime:**

```csharp
string dateString = "2023-03-08";
DateTime dateTime = DateTime.Parse(dateString);
```

**13. Convert from a TimeSpan to a string:**

```csharp
TimeSpan timeSpan = new TimeSpan(1, 2, 3);
string timeSpanString = timeSpan.ToString();
```

**14. Convert from a string to a TimeSpan:**

```csharp
string timeSpanString = "01:02:03";
TimeSpan timeSpan = TimeSpan.Parse(timeSpanString);
```

**15. Convert from a Guid to a string:**

```csharp
Guid guid = Guid.NewGuid();
string guidString = guid.ToString();
```

**16. Convert from a string to a Guid:**

```csharp
string guidString = "01234567-89ab-cdef-0123-456789abcdef";
Guid guid = Guid.Parse(guidString);
```

**17. Convert from a byte array to a string:**

```csharp
byte[] byteArray = new byte[] { 72, 101, 108, 108, 111 };
string str = System.Text.Encoding.UTF8.GetString(byteArray);
```

**18. Convert from a string to a byte array:**

```csharp
string str = "Hello";
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(str);
```

**19. Convert from a decimal to a string:**

```csharp
decimal decimalValue = 123.45m;
string str = decimalValue.ToString();
```

**20. Convert from a string to a decimal:**

```csharp
string str = "123.45";
decimal decimalValue = decimal.Parse(str);
```

**21. Convert from a double to a string:**

```csharp
double doubleValue = 123.45;
string str = doubleValue.ToString();
```

**22. Convert from a string to a double:**

```csharp
string str = "123.45";
double doubleValue = double.Parse(str);
```

**23. Convert from a float to a string:**

```csharp
float floatValue = 123.45f;
string str = floatValue.ToString();
```

**24. Convert from a string to a float:**

```csharp
string str = "123.45";
float floatValue = float.Parse(str);
```

**25. Convert from a long to a string:**

```csharp
long longValue = 1234567890;
string str = longValue.ToString();
```

**26. Convert from a string to a long:**

```csharp
string str = "1234567890";
long longValue = long.Parse(str);
```

**27. Convert from a short to a string:**

```csharp
short shortValue = 123;
string str = shortValue.ToString();
```

**28. Convert from a string to a short:**

```csharp
string str = "123";
short shortValue = short.Parse(str);
```

**29. Convert from a uint to a string:**

```csharp
uint uintValue = 123456789;
string str = uintValue.ToString();
```

**30. Convert from a string to a uint:**

```csharp
string str = "123456789";
uint uintValue = uint.Parse(str);
```

**31. Convert from a ulong to a string:**

```csharp
ulong ulongValue = 1234567890123456;
string str = ulongValue.ToString();
```

**32. Convert from a string to a ulong:**

```csharp
string str = "1234567890123456";
ulong ulongValue = ulong.Parse(str);
```

**33. Convert from a char to a string:**

```csharp
char charValue = 'a';
string str = charValue.ToString();
```

**34. Convert from a string to a char:**

```csharp
string str = "a";
char charValue = str[0];
```

**35. Convert from a bool to a string:**

```csharp
bool boolValue = true;
string str = boolValue.ToString();
```

**36. Convert from a string to a bool:**

```csharp
string str = "true";
bool boolValue = bool.Parse(str);
```

**37. Convert from an array to a string:**

```csharp
int[] array = new int[] { 1, 2, 3 };
string str = string.Join(",", array);
```

**38. Convert from a string to an array:**

```csharp
string str = "1,2,3";
int[] array = str.Split(',').Select(int.Parse).ToArray();
```

**39. Convert from a dictionary to a string:**

```csharp
Dictionary<string, int> dictionary = new Dictionary<string, int>();
string str = JsonConvert.SerializeObject(dictionary);
```

**40. Convert from a string to a dictionary:**

```csharp
string str = "{\"name\":\"John\",\"age\":30}";
Dictionary<string, int> dictionary = JsonConvert.DeserializeObject<Dictionary<string, int>>(str);
```

**41. Convert from a list to a string:**

```csharp
List<int> list = new List<int> { 1, 2, 3 };
string str = string.Join(",", list);
```

**42. Convert from a string to a list:**

```csharp
string str = "1,2,3";
List<int> list = str.Split(',').Select(int.Parse).ToList();
```

**43. Convert from a tuple to a string:**

```csharp
(int, string) tuple = (1, "John");
string str = JsonConvert.SerializeObject(tuple);
```

**44. Convert from a string to a tuple:**

```csharp
string str = "{\"Item1\":1,\"Item2\":\"John\"}";
(int, string) tuple = JsonConvert.DeserializeObject<ValueTuple<int, string>>(str);
```

**45. Convert from a nullable type to a string:**

```csharp
int? nullableInt = null;
string str = nullableInt.HasValue ? nullableInt.Value.ToString() : null;
```

**46. Convert from a string to a nullable type:**

```csharp
string nullableIntString = "123";
int? nullableInt = nullableIntString != null ? (int?)int.Parse(nullableIntString) : null;
```

**47. Convert from a nullable type to a non-nullable type using the null coalescing operator:**

```csharp
int? nullableInt = null;
int nonNullableInt = nullableInt ?? -1;
```

**48. Convert from a non-nullable type to a nullable type using the null-conditional operator:**

```csharp
int nonNullableInt = 123;
int? nullableInt = nonNullableInt?;
```

**49. Convert from a nullable type to a non-nullable type using the TryGetValue method:**

```csharp
int? nullableInt = 123;
bool success = nullableInt.TryGetValue(out int nonNullableInt);
```

**50. Convert from a non-nullable type to a nullable type using the HasValue property:**

```csharp
int nonNullableInt = 123;
bool hasValue = nonNullableInt.HasValue;
int? nullableInt = hasValue ? nonNullableInt : null;
```
