今日は値型と参照型について学びました。
以下のコードサンプルに1つ疑問があります。
class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
FunctionSB(sb);
Console.WriteLine(sb); //sb updated
customer c = new customer();
FunctionClass(c);
Console.WriteLine(c.s);//updated class value
String str = "";
FuntionString(str);
Console.WriteLine(str);//
}
private static void FunctionSB(StringBuilder sb)
{
sb.Append("sb updated");
}
private static void FunctionClass(customer c)
{
c.s = "updated class value ";
}
static void FuntionString(String str)
{
str = "updated value";
}
}
class customer
{
public string s;
}
ここでは、文字列ビルダーの値とクラス メンバー変数の値が更新されていますが、値が更新されていないのはなぜFuntionString(str);
ですかstr
? (参照として渡されないのはなぜですか?)