コンテンツにスキップ

プリミティブ型

書き方説明
sbyte byte short ushort int uint long ulong整数の型
float a = 1.5f; double b = 2.5;float と double
decimal a = 1.5m;decimal
char c = 'a'; bool b = true;文字と真偽
object o = "a"; string s = "b";object と string
書き方説明エラー理由代わりに
System.IntPtr p = System.IntPtr.Zero;生のポインタ幅の整数TUKI0102, TUKI0099環境intlong を使う
使えない型があるのは、実行環境にその型が無いからです(TUKI0102)。言語の側で禁じているわけではありません。

整数は 8 種すべて使えます。int より狭い整数は、演算の前に int へ上がります()。

using UnityEngine;
using Tsukimi;
public class TyPrimInts : TsukimiBehaviour
{
void Start()
{
sbyte a = 1; byte b = 2; short c = 3; ushort d = 4;
int e = 5; uint f = 6; long g = 7; ulong h = 8;
Debug.Log(a + b + c + d + e + (int)f + (int)g + (int)h); // => 36
}
}
using UnityEngine;
using Tsukimi;
public class TyPrimFloat : TsukimiBehaviour
{
void Start()
{
float a = 1.5f;
double b = 2.5;
Debug.Log(a + (float)b); // => 4
}
}

decimal のリテラルを書いて、double へ変換できます。

using UnityEngine;
using Tsukimi;
public class TyPrimDecimal : TsukimiBehaviour
{
void Start()
{
decimal a = 1.5m;
Debug.Log((double)a); // => 1.5
}
}

charbool も値として運ばれます。

using UnityEngine;
using Tsukimi;
public class TyPrimCharBool : TsukimiBehaviour
{
void Start()
{
char c = 'a';
bool b = true;
int code = c;
Debug.Log(code); // => 97
Debug.Log(b); // => true
}
}

objectstring は参照として運ばれます。

using UnityEngine;
using Tsukimi;
public class TyPrimObjStr : TsukimiBehaviour
{
void Start()
{
object o = "a";
string s = "b";
Debug.Log(o.ToString() + s); // => "ab"
}
}