Da du oben schriebst das du es ohne ref übergibst bin ich davon ausgegangen das du es einer Methode einer anderen Klasse übergibst.
Das Prinzip ist aber trotzdem das Selbe.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
A a = new A();
a.str = "Hello";
B b = new B();
b.value = a;
b.value.str = "World";
Console.WriteLine("{0} {1}!", a.str, b.value.str);
}
}
class A
{
public string str;
}
class B
{
public A value;
}
}
[/PHP]
Gibt [i]World World![/i] aus weil hier
b.value = a;
b.value eine Referenz auf a zugewiesen wird da Klassen immer Referenzdatentypen sind.
Wenn man das nicht will muss man das Objekt kopieren. Eine Möglichkeit dazu wäre Serialisieren und wieder Deserialisieren oder zum Beispiel IClonable zu implementieren:
[PHP]
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
A a = new A();
a.str = "Hello";
B b = new B();
b.value = a.Clone() as A;
b.value.str = "World";
Console.WriteLine("{0} {1}!", a.str, b.value.str);
}
}
class A : ICloneable
{
public string str;
#region ICloneable Members
public object Clone()
{
return this.MemberwiseClone();
}
#endregion
}
class B
{
public A value;
}
}
Wenn eine flache Kopie nicht ausreichen sollte muss man dann halt in der Clone Methode die entsprechenden Member manuell kopieren.