C#
ラムダ式
例)足し算の結果を返す
Func<int, int, int> Sum = (x, y) => x + y;
int x = Sum(3, 4); // x=7
Func<int, int, string> Sum = (x, y) => (x + y).ToString();
string x = Sum(3, 4) // x=7
ジェネリック コードのdefault
T temp = default(T);
↑Tが参照型ならnull, 数値型なら0を代入
[参考]
Null 合体演算子
null規定値だったら違う値にしたい時に使える
例) x = value ?? -1;
→x = (value==null ? -1 : value)と同じ感じ。
[参考]
outとrefの違い
ref:渡す前に初期化しないとエラー
out:渡した先で初期化しないとエラー
コンパイラ的にはrefもoutも同じ扱いなので
同じメソッドをオーバーロードできない。
[参考]
参照型の値渡し
class PassingRefByVal
{
static void Change(int[] pArray) { pArray[0] = 888; // This change affects the original element. pArray = new int[5] {-3, -1, -2, -3, -4}; // This change is local. System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]); }
static void Main() { int[] arr = {1, 4, 5}; System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr [0]);
Change(arr); System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr [0]); }
}
出力
Inside Main, before calling the method, the first element is: 1
Inside the method, the first element is: -3
Inside Main, after calling the method, the first element is: 888
★参照のコピーを渡すので、参照先をの値を変更したら変更後の値は反映される
★参照のコピーを渡すので、新たにnewすると違う参照をみるので呼び元の関数に影響はでない
参照型の参照渡し
class PassingRefByRef
{
static void Change(ref int[] pArray) { // Both of the following changes will affect the original variables: pArray[0] = 888; pArray = new int[5] {-3, -1, -2, -3, -4}; System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]); } static void Main() { int[] arr = {1, 4, 5}; System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr[0]);
Change(ref arr); System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr[0]); }
}
Inside Main, before calling the method, the first element is: 1
Inside the method, the first element is: -3
Inside Main, after calling the method, the first element is: -3
★参照を渡す(呼び元の変数をそのまま利用する)
メンバの修飾子
修飾子 | 説明 |
abstruct | メンバメソッドは実装されず、派生先で実装する |
extern | メンバメソッドは外部のDLL内で実装される |
new | 類似したメンバまたは基本クラス内のメンバを隠蔽する |
override | 派生クラス内のメソッドが基本クラス内の仮想メソッドをオーバーライドすることを示す |
readonly | 読み取り専用フィールドは宣言時またはコンストラクタ内で初期化される |
sealed | メソッドは継承を通じて改変することができない |
static | メンバはインスタンスではなく、クラスに属する |
virtual | メンバメソッドは派生クラス内でオーバーライドできる |
volatile | フィールドは、基本的に即時実行される |
デストラクタ
Finalize を暗黙的に呼び出します。←ここが重要
Finalizeはリソースを開放する処理を行う。
[参考]
sealedクラス
継承が禁止になる
例)
sealed class B : A {}
[参考]
internal
同一プロジェクト内でのみ参照
[参考]
using ステートメント
スコープを宣言しておき、抜けるときにDispose()をコールすることを保障する。
例)
using (Font MyFont = new Font("Arial", 10.0f)) { // use MyFon } // compiler will call Dispose on MyFont and MyFont2
[参考]
基本型
値型 | .NET Framework 型 | 例 | サフィックス | 有効範囲 | 有効桁数 | 他の型への暗黙の変換 |
bool | System.Boolean | true | なし | true/false | - | なし |
byte | System.Byte | 20 | なし | 0 ~ 255 | - | short、ushort、int、uint、long、ulong、float、double、decimal |
char | System.Char | '\u0058' | なし | U+0000 ~ U+ffff | - | ushort、int、uint、long、ulong、float、double、decimal |
decimal | System.Decimal | 300.5m | m/M | ±1.0 × 10^-28 ~ ±7.9 × 10^28 | 28 ~ 29 | なし |
double | System.Double | 3.2d | d/D | ±5.0 × 10^-324 ~ ±1.7 × 10^308 | 15 ~ 16 | なし |
float | System.Single | 3.5f | f/F | ±1.5 × 10^-45 ~ ±3.4 × 10^38 | 7 | double |
int | System.Int32 | 325 | なし | -2,147,483,648 ~ 2,147,483,647 | - | long、float、double、decimal |
long | System.Int64 | 4294967296L | (l/)L | -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 | - | float、double、decimal |
sbyte | System.SByte | 127 | なし | -128 ~ 127 | - | short、int、long、float、double、decimal |
short | System.Int16 | 32767 | なし | -32,768 ~ 32,767 | - | int、long、float、double、decimal |
uint | System.UInt32 | 123U | u/U | 0 ~ 4,294,967,295 | - | long、ulong、float、double、decimal |
ulong | System.UInt64 | 123UL | (l/)L,u/U,ul/UL,lu/LU | 0 ~ 18,446,744,073,709,551,615 | - | float、double、decimal |
ushort | System.UInt16 | 65535 | なし | 0 ~ 65,535 | - | int、uint、long、ulong、float、double、decimal |
- 最終更新:2009-11-30 23:45:52