C# 개체를 int로

WBOY
풀어 주다: 2024-09-03 15:05:10
원래의
704명이 탐색했습니다.

C#의 개체는 해당하는 32비트 부호 있는 정수로 변환될 수 있고, C#의 개체는 해당하는 32비트 부호 있는 정수로 변환될 수 있습니다. C#에서는 Convert.ToInt32(Object) 함수라는 함수를 사용합니다. 여기서 Object는 해당하는 32비트 부호 있는 정수로 변환될 특정 개체의 값을 나타냅니다. int32로도 표현되며, 이 함수를 사용하여 변환된 특정 객체의 값은 32비트 부호 있는 정수 범위 내에 있어야 하며, 주어진 객체에 대해 이 함수에 의해 이에 상응하는 32비트 부호 있는 정수가 반환됩니다. 이번 주제에서는 C# 객체를 int로 변환하는 방법에 대해 알아보겠습니다.

C#에서 Object를 정수로 변환하는 구문은 다음과 같습니다.

int Convert.ToInt32(object value);
로그인 후 복사

여기서 Object는 int32로도 표시되는 해당 32비트 부호 있는 정수로 변환될 특정 개체의 값을 나타냅니다.

C#에서 Object를 정수로 변환하는 단계는 다음과 같습니다.

  • C#의 개체는 해당하는 32비트 부호 있는 정수로 변환될 수 있으며, C#의 개체를 해당하는 32비트 부호 있는 정수로 변환하려면 C#에서 Convert.ToInt32(Object라는 함수를 사용합니다. ) 기능을 사용하세요.
  • Convert.ToInt32(Object) 함수에 매개변수로 전달된 객체는 특정 객체의 값을 나타내며, 이는 int32로도 표시되는 해당 32비트 부호 있는 정수로 변환됩니다.
  • 이 함수를 사용하여 변환된 특정 객체의 값은 32비트 부호 있는 정수 범위 내에 있어야 합니다.
  • 주어진 개체에 대한 함수별로 동등한 32비트 부호 있는 정수가 Convert.ToInt32(Object) 함수로 반환됩니다.

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라는 클래스가 정의됩니다. 그런 다음 서로 다른 데이터 유형 개체를 저장하기 위해 첫 번째와 두 번째라는 두 개체가 정의되는 기본 메서드가 호출됩니다. 그런 다음 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라는 클래스가 정의됩니다. 그런 다음 서로 다른 데이터 유형 개체를 저장하기 위해 첫 번째와 두 번째라는 두 개체가 정의되는 기본 메서드가 호출됩니다. 그런 다음 GetType() 함수를 사용하여 각 객체의 데이터 유형을 얻어 화면에 표시합니다. 그런 다음 Convert.ToInt32() 함수를 사용하여 각 개체를 동등한 정수 유형으로 변환합니다. 그러면 각 객체의 변환된 값이 화면에 출력으로 표시됩니다. 그런 다음 GetType() 함수를 사용하여 얻은 해당 데이터 유형이 화면에 출력으로 표시됩니다.

결론

이 글에서는 프로그래밍 예제와 그 출력을 통해 C#에서 객체를 정수로 변환하는 정의, 구문, 단계를 통해 Convert.ToInt32() 함수를 사용하여 C#에서 객체를 정수로 변환하는 개념을 배웠습니다. .

위 내용은 C# 개체를 int로의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!