I've come across a struct in legacy code that I feel should be redefined as a class. It doesn't fit the general guidelines for choosing between a class and a struct.
While I'm in here, I would like to better understand if a struct that contains methods with reference type parameters, such as an interface, are in any case possibly less efficient due to boxing and unboxing?
My understanding is that value types are boxed when cast to a reference type or to an interface they implement, and unboxed when cast back to a value type, but I am unclear about the implications of using reference types within methods of a struct.
Given a struct and a static class:
public struct Foo {
public static string ResolveSomething(IBar someInterface, int baz) {
return someInterface.Calculate(baz);
}
}
public static class FooClass {
public static string ResolveSomething(IBar someInterface, int baz) {
return someInterface.Calculate(baz);
}
}
Does the following use of the struct vs. class have any general implications with regards to memory use?
Foo.ResolveSomething(someImplementation, 2);
FooClass.ResolveSomething(someImplementation, 2);