C#算術データの型

C#で使われる代表的な算術データの型と、最小値(MinValue)と最大値(MaxValue)の説明です。

前田稔(Maeda Minoru)の超初心者のプログラム入門

プロジェクトの設定

  1. フォルダーに次のファイルを格納して下さい。
    /**********************************/
    /*★ Data Type 一覧     前田 稔 ★*/
    /**********************************/
    using System;
    
    class Text
    {
        public static void Main()
        {
            string format = "{0, -8}:{1}~{2}";
            string dot = "----------------";
    
            Console.WriteLine(format, "sbyte",sbyte.MinValue, sbyte.MaxValue);
            Console.WriteLine(format, "short", short.MinValue, short.MaxValue);
            Console.WriteLine(format, "int", int.MinValue, int.MaxValue);
            Console.WriteLine(format, "long", long.MinValue, long.MaxValue);
            Console.WriteLine(dot);
            Console.WriteLine(format, "byte", byte.MinValue, byte.MaxValue);
            Console.WriteLine(format, "ushort", ushort.MinValue, ushort.MaxValue);
            Console.WriteLine(format, "uint", uint.MinValue, uint.MaxValue);
            Console.WriteLine(format, "ulong", ulong.MinValue, ulong.MaxValue);
            Console.WriteLine(dot);
            Console.WriteLine(format, "float", float.MinValue, float.MaxValue);
            Console.WriteLine(format, "double", double.MinValue, double.MaxValue);
            Console.WriteLine(dot);
            Console.WriteLine(format, "decimal", decimal.MinValue, decimal.MaxValue);
            Console.WriteLine(dot);
        }
    }
    
  2. それぞれのデータの型の最小値と最大値を表す表記方法が決められています。
    例えば、int.MinValue が int の最小値で、int.MaxValue が int の最大値です。
    Console.WriteLine(format, "int", int.MinValue, int.MaxValue);
  3. {0, -8} は0番目の引数を、8桁で左詰(-8)に編集する指定です。
    8桁で右詰のときは {0, 8} とします。
    string format = "{0, -8}:{1}~{2}";

【結果】

プログラムの実行結果です。
c:\DATA\C#>DataType
sbyte   :-128~127
short   :-32768~32767
int     :-2147483648~2147483647
long    :-9223372036854775808~9223372036854775807
----------------
byte    :0~255
ushort  :0~65535
uint    :0~4294967295
ulong   :0~18446744073709551615
----------------
float   :-3.402823E+38~3.402823E+38
double  :-1.79769313486232E+308~1.79769313486232E+308
----------------
decimal :-79228162514264337593543950335~79228162514264337593543950335
----------------

超初心者のプログラム入門(C# Frame Work)