C# 对象转int

WBOY
发布: 2024-09-03 15:05:10
原创
659 人浏览过

C# 中的对象可以转换为其等效的 32 位有符号整数,并且能够将 C# 中的对象转换为其等效的 32 位有符号整数。我们使用 C# 中名为 Convert.ToInt32(Object) 的函数,其中 Object 表示要转换为等效 32 位有符号整数的特定对象的值。也表示为int32,使用该函数转换的特定对象的值应在32位有符号整数范围内,并且该函数对于给定的Object返回等效的32位有符号整数。在本主题中,我们将学习 C# 对象到 int 的知识。

C# 中声明对象到整数转换的语法如下:

int Convert.ToInt32(object value);
登录后复制

其中 Object 表示要转换为等效 32 位有符号整数的特定对象的值,也表示为 int32。

C# 中将对象转换为整数的步骤如下:

  • C# 中的对象可以转换为其等效的 32 位有符号整数,并且为了能够将 C# 中的对象转换为其等效的 32 位有符号整数,我们使用 C# 中名为 Convert.ToInt32(Object ) 函数。
  • 作为参数传递给 Convert.ToInt32(Object) 函数的对象表示特定对象的值,该值将被转换为其等效的 32 位有符号整数,也表示为 int32。
  • 使用该函数转换的具体对象的值应在32位有符号整数范围内。
  • 对于给定对象,Convert.ToInt32(Object) 函数将返回等效的 32 位有符号整数。

C# 对象转 int 的示例

以下是下面提到的示例

示例#1

C# 程序确定给定对象的类型,然后将给定对象的值转换为其等效的有符号整数,并将输出显示在屏幕上:

代码:

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();
}
}
}
登录后复制

上述程序的输出如下图所示:

C# 对象转int

在上面的程序中,定义了一个名为std的命名空间。然后定义一个名为check的类。然后调用main方法,在该方法中定义了first和second这两个对象来存储不同数据类型的对象。然后使用GetType()函数获取每个对象的数据类型并将其显示在屏幕上。然后使用 Convert.ToInt32() 函数将每个对象转换为其等效的整数类型。然后每个对象的转换值将作为输出显示在屏幕上。然后使用 GetType() 函数获取的各自的数据类型将作为输出显示在屏幕上。

示例#2

C# 程序确定给定对象的类型,然后将给定对象的值转换为其等效的有符号整数,并将输出显示在屏幕上:

代码:

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();
}
}
}
登录后复制

上述程序的输出如下图所示:

C# 对象转int

在上面的程序中,定义了一个名为std的命名空间。然后定义一个名为check的类。然后调用main方法,在该方法中定义了first和second这两个对象来存储不同数据类型的对象。然后使用GetType()函数获取每个对象的数据类型并将其显示在屏幕上。然后使用 Convert.ToInt32() 函数将每个对象转换为其等效的整数类型。然后每个对象的转换值将作为输出显示在屏幕上。然后使用 GetType() 函数获取的各自的数据类型将作为输出显示在屏幕上。

结论

在本文中,我们通过编程示例及其输出了解了使用 Convert.ToInt32() 函数在 C# 中将对象转换为整数的概念、语法和步骤,以及在 C# 中将对象转换为整数的步骤.

以上是C# 对象转int的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!