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
9
10
11
12
13
14
15
16
17
18
19
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
// Declare some variables
object
container1 =
5;
object
container2 =
"Five";
// Print the results on the console
Console.WriteLine("The value of containter1 is: " + container1);
Console.WriteLine("The value of containter2 is: " + container2);
Console.Read();
}
}
}
|
And the output would be:
The value of container1 is: 5
The value of container2 is: Five
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: