C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#のint.Parse()とConvert.ToInt32の主な違いは何ですか?


C#でint.ParseまたはConvert.ToInt32メソッドを使用して、数値の文字列表現を整数に変換します。文字列を変換できない場合、int.ParseまたはConvert.ToInt32メソッドは例外を返します

Convert.ToInt32はnull値を許可し、エラーをスローしませんInt.parseはnull値を許可せず、ArgumentNullExceptionエラーをスローします。

class Program {
   static void Main() {
      int res;
      string myStr = "5000";
      res = int.Parse(myStr);
      Console.WriteLine("Converting String is a numeric representation: " + res);
      Console.ReadLine();
   }
}

出力

Converting String is a numeric representation: 5000

class Program {
   static void Main() {
      int res;
      string myStr = null;
      res = Convert.ToInt32(myStr);
      Console.WriteLine("Converting String is a numeric representation: " + res);
      Console.ReadLine();
   }
}

出力

Converting String is a numeric representation: 0

class Program {
   static void Main() {
      int res;
      string myStr = null;
      res = int.Parse(myStr);
      Console.WriteLine("Converting String is a numeric representation: " + res);
      Console.ReadLine();
   }
}

出力

Unhandled exception. System.ArgumentNullException: Value cannot be null.

  1. MySQLのintとintegerの違いは何ですか?

    intは、MySQL5.0の整数の同義語です。これは、intとintegerの両方が内部的にint(11)を表すデモ表示です。 intデータ型でテーブルを作成する mysql> create table IntDemo    -> (    -> Id int    -> ); Query OK, 0 rows affected (1.04 sec) 表の説明は次のとおりです。クエリは次のとおりです mysql> desc IntDemo; 以下は出力です +-------+---------+--

  2. const int *、const int * const、およびint const *の違いは何ですか?

    ここでは、整数ポインター、整数定数、および整数定数ポインターに基づくいくつかの異なるタイプの変数宣言を確認します。 それらを決定するために、時計回り/スパイラルルールを使用します。用語を話し合うことで、ルールも理解できます。 const int * 。これは、これがポインター型変数であることをコンパイラーに通知するために使用され、定数intのアドレスを格納できます。時計のルールはこのように言っています- もう1つはconstint*constです。これは、これが1つの定数ポインター変数であり、別の定数整数のアドレスを格納できることを示すために使用されます。時計のルールを適用する