Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Casting between String and Object types in C#

The following code explains the Casting between String and Object types.

  1. using System;
  2. using System.Text;
  3.  
  4. namespace forgetCode
  5. {
  6.  
  7. class program
  8. {
  9. public static void Main()
  10. {
  11. // Assign string and then cast it to an object implictly.
  12. string value1 = "hi";
  13. object value2 = value1;
  14.  
  15. // Explicitly cast to string again from the object type.
  16. string value3 = (string)value2;
  17. Console.WriteLine(value3);
  18.  
  19. }
  20. }
  21. }


Output:
hi