1647855961
このガイドでは、C#メソッドのパラメーターとして参照型と値型を渡すことの違いを見ていきます。これを理解していなければ、間違った変数を変更して、何が起こったのかを理解するために何時間も費やすのは簡単です。
参照型変数には、ヒープとも呼ばれる、メモリに格納されているデータへの参照が含まれています。ヒープは、寿命の長いデータに最もよく使用されます。同じ参照データへの複数の変数ポイントを持つことができます。オブジェクトは参照型の例です。
var a = new Student();
var b = a;
上記の例では、両方の変数a
とb
がメモリ内の同じ学生オブジェクトを指します。を変更a
すると、も変更されb
ます。これは、a
とb
がデータを保存しているのではなく、データが保存されている場所への参照であるためです。
値型変数は、データへの参照ではなく、データを含む不変のデータです。多くの場合、値型の寿命は短くなります。それらは通常、スタックと呼ばれる領域のメモリに格納されます。スタックは、長期間存在する必要のないデータが存在する場所です。Structs、Int32、DateTime、およびDoubleは、値型の例です。
デフォルトでは、参照型変数をメソッドに渡すと、実際のデータではなく、参照のコピーが渡されます。参照タイプ内の値を変更すると、メソッド外の値も変更されます。これは、メモリ内の同じ参照位置を指しているためです。メソッド内の新しいオブジェクトに変数を割り当てようとすると、警告が発生します。これにより、変数がメモリ内の参照オブジェクトをポイントしなくなります。その後の変更は、元の参照オブジェクトには反映されません。次に例を示します。
class ReferenceTypeExample
{
static void Enroll(Student student)
{
student.Enrolled = true; // This changes the student variable that was passed in outside of the method.
student = new Student(); // This does not change the student variable outside of the method but creates a new reference. Since student now points to a new reference, the student variable outside of the method is no longer affected after this line.
student.Enrolled = false; // This changes the local student inside the method.
}
static void Main()
{
var student = new Student
{
Name = "Susan",
Enrolled = false
};
Enroll(student);
// student.Name is still Susan
// student.Enrolled is now true
}
}
public class Student {
public string Name {get;set;}
public bool Enrolled {get;set;}
}
値型がメソッドに渡されると、その値はメソッドに対してローカルであると見なされます。メソッド内で行われたことは、元の変数を変更しません。
class ReferenceTypeExample
{
static void Enroll(bool enrollmentStatus)
{
// This will not change any value outside the method.
enrollmentStatus = true;
}
static void Main()
{
var student = new Student
{
Name = "Susan",
Enrolled = false
};
Enroll(student.Enrolled);
// student.Enrolled still equals false.
}
}
public class Student {
public string Name {get;set;}
public bool Enrolled {get;set;}
}
これらの簡単なテクニックを知っていると、データの価値を変えるような間違いを防ぐことができます。
1647855961
このガイドでは、C#メソッドのパラメーターとして参照型と値型を渡すことの違いを見ていきます。これを理解していなければ、間違った変数を変更して、何が起こったのかを理解するために何時間も費やすのは簡単です。
参照型変数には、ヒープとも呼ばれる、メモリに格納されているデータへの参照が含まれています。ヒープは、寿命の長いデータに最もよく使用されます。同じ参照データへの複数の変数ポイントを持つことができます。オブジェクトは参照型の例です。
var a = new Student();
var b = a;
上記の例では、両方の変数a
とb
がメモリ内の同じ学生オブジェクトを指します。を変更a
すると、も変更されb
ます。これは、a
とb
がデータを保存しているのではなく、データが保存されている場所への参照であるためです。
値型変数は、データへの参照ではなく、データを含む不変のデータです。多くの場合、値型の寿命は短くなります。それらは通常、スタックと呼ばれる領域のメモリに格納されます。スタックは、長期間存在する必要のないデータが存在する場所です。Structs、Int32、DateTime、およびDoubleは、値型の例です。
デフォルトでは、参照型変数をメソッドに渡すと、実際のデータではなく、参照のコピーが渡されます。参照タイプ内の値を変更すると、メソッド外の値も変更されます。これは、メモリ内の同じ参照位置を指しているためです。メソッド内の新しいオブジェクトに変数を割り当てようとすると、警告が発生します。これにより、変数がメモリ内の参照オブジェクトをポイントしなくなります。その後の変更は、元の参照オブジェクトには反映されません。次に例を示します。
class ReferenceTypeExample
{
static void Enroll(Student student)
{
student.Enrolled = true; // This changes the student variable that was passed in outside of the method.
student = new Student(); // This does not change the student variable outside of the method but creates a new reference. Since student now points to a new reference, the student variable outside of the method is no longer affected after this line.
student.Enrolled = false; // This changes the local student inside the method.
}
static void Main()
{
var student = new Student
{
Name = "Susan",
Enrolled = false
};
Enroll(student);
// student.Name is still Susan
// student.Enrolled is now true
}
}
public class Student {
public string Name {get;set;}
public bool Enrolled {get;set;}
}
値型がメソッドに渡されると、その値はメソッドに対してローカルであると見なされます。メソッド内で行われたことは、元の変数を変更しません。
class ReferenceTypeExample
{
static void Enroll(bool enrollmentStatus)
{
// This will not change any value outside the method.
enrollmentStatus = true;
}
static void Main()
{
var student = new Student
{
Name = "Susan",
Enrolled = false
};
Enroll(student.Enrolled);
// student.Enrolled still equals false.
}
}
public class Student {
public string Name {get;set;}
public bool Enrolled {get;set;}
}
これらの簡単なテクニックを知っていると、データの価値を変えるような間違いを防ぐことができます。