In .NET, each data type is associated with a class, which offers few methods for the variable (object).
All of the data type classes are the sub classes of an ultimate base class called “Object”.
All the data type classes are the members of “System” namespace.
You can see the data types along with the respective class names in the following table.
Data Type |
Class Name |
sbyte |
System.SByte |
byte |
System.Byte> |
short |
System.Int16 |
ushort |
System.UInt16 |
int |
System.Int32 |
uint |
System.UInt32 |
long |
System.Int64 |
ulong |
System.UInt64 |
float |
System.Single |
double |
System.Double |
bool |
System.Boolean |
char |
System.Char |
decimal |
System.Decimal |
string |
System.String |
The “Object” class, which is a base class for all other data type classes, offers few methods, which can be accessible by the instances of data type classes (variables).
You can observe the methods offered by “Object” class in the following table.
Method |
Description |
Equals(value) |
Checks the equality of the value of the object with the given argument value. If both are equal, then returns “true”, otherwise returns “false”.
Ex:
MyObj.Equals(AnotherObject);
In this example, “MyObj” will be compared with “AnotherObject”.
|
GetType() |
Gets the name of the class, for which, it is declared.
Ex:
MyObj.GetType();
|
ToString() |
Converts the value of the object and returns it.
Ex:
MyObj.ToString();
In this example, the value of “MyObj” will be converted as string type, and that string value will be returned.
|
Demo on “Object” class methods
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ObjectClassMethods
{
class Program
{
static void Main(string[] args)
{
int x = 100;
int y = 100;
Console.WriteLine("x is " + x);
Console.WriteLine("y is " + y);
if (x.Equals(y))
Console.WriteLine("x is equal to y");
else
Console.WriteLine("x is not equal to y");
Console.WriteLine("x is the type of " + x.GetType());
string s = x.ToString();
Console.WriteLine("s is " + s);
Console.Read();
}
}
}