プリミティブ型
書ける形(5)
Section titled “書ける形(5)”| 書き方 | 説明 |
|---|---|
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 |
書けない形(1)
Section titled “書けない形(1)”| 書き方 | 説明 | エラー | 理由 | 代わりに |
|---|---|---|---|---|
System.IntPtr p = System.IntPtr.Zero; | 生のポインタ幅の整数 | TUKI0102, TUKI0099 | 環境 | int か long を使う |
使えない型があるのは、実行環境にその型が無いからです(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 }}float と double
Section titled “float と double”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
Section titled “decimal”decimal のリテラルを書いて、double へ変換できます。
using UnityEngine;using Tsukimi;
public class TyPrimDecimal : TsukimiBehaviour{ void Start() { decimal a = 1.5m; Debug.Log((double)a); // => 1.5 }}char と bool も値として運ばれます。
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 }}object と string
Section titled “object と string”object と string は参照として運ばれます。
using UnityEngine;using Tsukimi;
public class TyPrimObjStr : TsukimiBehaviour{ void Start() { object o = "a"; string s = "b"; Debug.Log(o.ToString() + s); // => "ab" }}