아래 코드는 컴파일하고 의도 한대로 작동합니다. C에서 문자열을 매개 변수로 사용하는 방법 #
class MyClass1
{
public void test()
{
string one = "testString1";
Console.WriteLine("MyClass1: " + one);
new MyClass2().test(one);
Console.WriteLine(one); //again testString1 is printed.
}
}
class MyClass2
{
public void test(string two)
{
Console.WriteLine("Test method");
Console.WriteLine(two);
two = "pilot";
Console.WriteLine(two);
}
}
나는이 추론 모든
은 다음과 같습니다test
방법으로 문자열에 할당
값은 local to that function
내가이 ref or out
를 사용하는 경우에만 변경 사항이 반영됩니다.
질문 :
우리 모두 문자열이 reference
유형인지 알고 (이 타입이기 때문에, 문자열)
그래서 모든 reference types
에 대해 : 해당 개체 주위에 전달 변경 반영되어야 하는가? (예를 들어, 같은 예를 들어 클래스의 객체를 전달하면 어떤 변경 사항도 바로 반영됩니다.)
여기에이 규칙이 적용되지 않는 이유는 무엇입니까?
아무도 후드에서 일어나는 일을 이해할 수 있습니까?
여기 - http://www.yoda.arachsys.com/csharp/parameters.html – shahkalpesh
@shahkalpesh : 멋진 기사. 도움이되었다. 감사. –