Please check out these two small examples:
public struct Struct
{
public readonly string StringValue;
public readonly int IntValue;
public Struct(string stringValue)
{
this.StringValue = stringValue;
this.IntValue = this.StringValue.GetHashCode(); // Just ignore this assignment, please :).
}
}
public class Class
{
public readonly string StringValue;
public readonly int IntValue;
public Class(string stringValue)
{
this.StringValue = stringValue;
this.IntValue = this.StringValue.GetHashCode(); // Just ignore this assignment, please :).
}
}
From what I know if I pass struct as a parameter it will make a copy of it someObject.Method(this.cachedStruct);
- which in turn will make a copy of a StringValue
as string
s are immutable it will allocate array in memory for this string which is quite costly operation [pardon me if I made a mistake, please correct me in such a case].
So in this instance I assume it will be faster to use instances of the Class
rather than copying StringValue
each time. QUESTION: Are my assumptions correct and Class
is a better choice for this kind of a situation or is it still more beneficial and faster to use Struct
?
Since String
and string
are the same. ref - What is the difference between String and string in C#?.
I cannot use String
in struct to prevent it from creating a new string
object when struct has been passed as a parameter.
To better address the issue, here is another example. Sorry if it's a bit rough example.
public struct Struct
{
public string StringValue;
public int IntValue;
public Struct(string stringValue)
{
this.StringValue = stringValue;
this.IntValue = this.StringValue.GetHashCode(); // Just ignore this assignment, please :).
}
}
public class Class
{
public string StringValue;
public int IntValue;
private Struct _niceStruct;
public void Accept(Struct someStruct)
{
someStruct.StringValue = "Something new"; // In this case even if I don't change the value, it was already allocated.
// Because of copying of struct when it was passed as a parameter.
Console.WriteLine(this._niceStruct.StringValue); // "Something new".
}
public Class(string stringValue)
{
this.StringValue = stringValue;
this.IntValue = this.StringValue.GetHashCode(); // Just ignore this assignment, please :).
this._niceStruct = new Struct("Old value");
this.Accept(this._niceStruct);
Console.WriteLine(this._niceStruct.StringValue); // "Old value".
// String value was left unaffected.
// Which [even in the case I wouldn't change the value in the method] means that a new `string` object was allocated when a copy of `Struct` was created.
// When using `Class` in the same manner - it would change the `string`.
// New `string` object won't be allocated if value is unchanged.
// I assume that `string` allocation might be more costly than `Class` allocation in this instance.
}
}
My understanding is that if I use Struct
without changing string
value it will still allocate memory for StringValue
each time I pass it as a parameter. While when using Class
and passing its instance as a parameter - it will pass Class
instance as a reference type without copying values, thus not allocating any memory for StringValue
.
My question might seem similar to C#, struct vs class, faster? [duplicate] and other questions (I believe I have gone through them all (the ones that struct vs class performance)) but none of them have given me an answer.
Struct
isn't immutable and I changeStringValue
after I have passed instance as a parameter, wouldn't changes only apply to one string, while the other would be left unaffected, retaining it's previous value?