Object variable type is a special type, the parent of all other types in the .NET Framework. It can accept values from any other type in C#, and we declare it using the word object.
In the following example you can see how we can assign any type to an object variable type:
1 2 3 4 5 6 7 8 |
// Declare some variables object container1 = 5; object container2 = "Five"; // Print the results on the console Console.WriteLine("The value of container1 is: " + container1); Console.WriteLine("The value of container2 is: " + container2); Console.Read(); |
And the output would be:
As you can notice, the object variable can be very versatile, and we can use it as a universal data container.
The concepts explained in this lesson are also shown visually as part of the following video: