C# 物件轉int

WBOY
發布: 2024-09-03 15:05:10
原創
658 人瀏覽過

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學習者快速成長!