変数

整数型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("sbyte : {0}~{1}", sbyte.MinValue, sbyte.MaxValue);
            Console.WriteLine("byte  : {0}~{1}", byte.MinValue, byte.MaxValue);
            Console.WriteLine("short : {0}~{1}", short.MinValue, short.MaxValue);
            Console.WriteLine("ushort: {0}~{1}", ushort.MinValue, ushort.MaxValue);
            Console.WriteLine("int   : {0}~{1}", int.MinValue, int.MaxValue);
            Console.WriteLine("uint  : {0}~{1}", uint.MinValue, uint.MaxValue);
            Console.WriteLine("long  : {0}~{1}", long.MinValue, long.MaxValue);
            Console.WriteLine("ulong : {0}~{1}", ulong.MinValue, ulong.MaxValue);
            Console.ReadKey();
        }
    }
}
 
ビット長 範囲
sbyte 8 -128~127
byte 8 0~255
short 16 -32768~32767
ushort 16 0~65535
int 32 -2147483648~2147483647
uint 32 0~4294967295
long 64
ulong 64


浮動小数点型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("float :{0}~{1}", float.MinValue, float.MaxValue);
            Console.WriteLine("double:{0}~{1}", double.MinValue, double.MaxValue);
            Console.ReadKey();
        }
    }
}
 
ビット長 範囲
float 32 -3.402823E+38~3.402823E+38
double 64 -1.7976313486232E+308~1.7976313486232E+308

decimal型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {
            decimal d;
 
            // 小数の設定をする場合はMを設定
            d = 10.5m;
            Console.WriteLine("d1 = {0}", d);
 
            // 整数の設定
            d = 20;
            Console.WriteLine("d2 = {0}", d);
 
            // 
            Console.WriteLine("decimal:{0}~{1}", decimal.MinValue, decimal.MaxValue);
            Console.ReadKey();
        }
    }
}
 
ビット長 範囲
decimal 128 -79228162514264337593543950335~79228162514264337593543950335

decimalは小数の計算を高精度で計算することが可能

文字型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            char a, b, c, d;
 
            a = 'あ';
            b = '\x3067';
            c = 'か';
            d = '\u304B';
 
            Console.Write("{0}\n", a);
            Console.Write("{0}\n", b);
            Console.Write("{0}\n", c);
            Console.Write("{0}\n", d);
 
            Console.ReadKey();
        }
    }
}
 

unicode、文字と別にc言語と同様にエスケープ文字を使用することができます

論理型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication11
{
    class Program
    {
        static void Main(string[] args)
        {
            bool a, b;
 
            a = true;
            b = false;
 
            Console.WriteLine("a = {0, 6}, a_str= {1, 6}", a, a.ToString());
            Console.WriteLine("b = {0, 6}, b_str= {1, 6}", b, b.ToString());
            Console.ReadKey();
        }
    }
}
 
bool型はtrue、falseを設定するがc言語と同様に0や1を設定はできません

スコープ

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication12
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10;
            {
                int b = 20;
                {
                    // c
                    int c = 30;
                    Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);
                }
 
                // b 
                Console.WriteLine("a = {0}, b = {1}", a, b);
            }
 
            // a
            Console.WriteLine("a = {0}", a);
            Console.ReadKey();
 
        }
    }
}
 
{ ~ } で挟むことで変数の有効範囲を指定できます

型変換

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication13
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            long b;
 
            b = 10;
 
            // サイズが小さい型へ代入する場合はcastする
            a = (int)b;
 
            Console.WriteLine("a = {0}, b = {1}", a, b);
            Console.ReadKey();
 
        }
    }
}
 

列挙型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication14
{
    class Program
    {
        enum TEST
        {
            Test1 = 10
          , Test2 = 20
          , Test3 = 30
        };
        static void Main(string[] args)
        {
            Console.WriteLine(" Test1 = {0}", (int)TEST.Test1);
            Console.WriteLine(" Test2 = {0}", (int)TEST.Test2);
            Console.WriteLine(" Test3 = {0}", (int)TEST.Test3);
            Console.ReadKey();
        }
    }
}
 

列挙型は新しいリスト型のようなもので型と名前を定義する
列挙型に値を設定することも可能

オブジェクト型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication15
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10;
            long b = 1000;
            object o;
 
            // 
            o = a;
 
            Console.WriteLine(" o = {0}", o);
 
            // object型はどのデータ型も格納可能
            o = b;
            Console.WriteLine(" o = {0}", o);
            Console.ReadKey();
        }
    }
}
 

文字列型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication16
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "12345";
            char c;
            string mmm;
            Console.WriteLine("str = {0}", str);
            Console.WriteLine("length = {0}", str.Length);
 
            c = str[3];
            Console.WriteLine("c = {0}", c);
 
            mmm = String.Copy(str);
            Console.WriteLine("str = {0}", mmm);
            Console.ReadKey();
        }
    }
}
 


最終更新:2011年03月06日 12:44