Home > Article > Backend Development > C# object to int
An object in C# can be converted into its equivalent 32 bits signed integer and to be able to convert an object in C# to its equivalent 32 bits signed integer. We make use of a function in C# called Convert.ToInt32(Object) function where Object represents the value of the specific object which is to be converted into its equivalent 32 bits signed integer. It is also represented as int32, and the value of the specific object converted using this function should be within the range of 32 bits signed integer, and an equivalent 32 bits signed integer is returned by this function for the given Object. In this topic, we are going to learn about C# object to int.
The syntax to declare Object to integer conversion in C# is as follows:
int Convert.ToInt32(object value);
where Object represents the value of the specific object which is to be converted into its equivalent 32 bits signed integer, also represented as int32.
Steps to convert Object to integer in C# is as follows:
Here are the following examples mention below
C# program to determine the type of a given object and then convert the value of a given object to its equivalent signed integer and display the output on the screen:
Code:
using System.Text; using System; //defining a namespace called std namespace std { //defining a class called check class check { //main method is called static void Main() { //an object called first is defined object first = 'S'; //an object called second is defined object second = 10.23456m; //obtaining the data type of each object using GetType() function Console.WriteLine("The type of the first object is: {0}", first.GetType()); Console.WriteLine("The type of the first object is: {0}", first.GetType()); Console.WriteLine("\n"); //using Convert.ToInt32() function to convert the first and second objects to their equivalent integer types int firstresult = Convert.ToInt32(first); int secondresult = Convert.ToInt32(second); //displaying the value and type of the equivalent integer types of first and second objects Console.WriteLine("The value of first object after converting it to integer using Convert.ToInt32() function is: {0}", firstresult); Console.WriteLine("The type of first object after converting it to integer using Convert.ToInt32() function is: {0}", firstresult.GetType()); Console.WriteLine("\n"); Console.WriteLine("The value of second object after converting it to integer using Convert.ToInt32() function is: {0}", secondresult); Console.WriteLine("The type of second object after converting it to integer using Convert.ToInt32() function is: {0}", secondresult.GetType()); Console.ReadLine(); } } }
The output of the above program is as shown in the snapshot below:
In the above program, a namespace called std is defined. Then a class called check is defined. Then the main method is called within which the two objects called first and second are defined to store different data type objects. Then the data type of each object is obtained by using the GetType() function and is displayed on the screen. Then the Convert.ToInt32() function is used to convert each object to its equivalent integer types. Then the converted values of each object are displayed as the output on the screen. Then their respective data types obtained using the GetType() function are displayed as the output on the screen.
C# program to determine the type of a given object and then convert the value of a given object to its equivalent signed integer and display the output on the screen:
Code:
using System.Text; using System; //defining a namespace called std namespace std { //defining a class called check class check { //main method is called static void Main() { //an object called first is defined object first = 12.34f; //an object called second is defined object second = 10.45m; //obtaining the data type of each object using GetType() function Console.WriteLine("The type of the first object is: {0}", first.GetType()); Console.WriteLine("The type of the first object is: {0}", first.GetType()); Console.WriteLine("\n"); //using Convert.ToInt32() function to convert the first and second objects to their equivalent integer types int firstresult = Convert.ToInt32(first); int secondresult = Convert.ToInt32(second); //displaying the value and type of the equivalent integer types of first and second objects Console.WriteLine("The value of first object after converting it to integer using Convert.ToInt32() function is: {0}", firstresult); Console.WriteLine("The type of first object after converting it to integer using Convert.ToInt32() function is: {0}", firstresult.GetType()); Console.WriteLine("\n"); Console.WriteLine("The value of second object after converting it to integer using Convert.ToInt32() function is: {0}", secondresult); Console.WriteLine("The type of second object after converting it to integer using Convert.ToInt32() function is: {0}", secondresult.GetType()); Console.ReadLine(); } } }
The output of the above program is as shown in the snapshot below:
In the above program, a namespace called std is defined. Then a class called check is defined. Then the main method is called within which the two objects called first and second are defined to store different data type objects. Then the data type of each object is obtained by using the GetType() function and is displayed on the screen. Then the Convert.ToInt32() function is used to convert each object to its equivalent integer types. Then the converted values of each object are displayed as the output on the screen. Then their respective data types obtained using the GetType() function are displayed as the output on the screen.
In this article, we have learned the concept of conversion of an object to an integer in C# using Convert.ToInt32() function through definition, syntax, and steps to convert an object to integer in C# through programming examples and their outputs.
The above is the detailed content of C# object to int. For more information, please follow other related articles on the PHP Chinese website!