Is there any sense in defining a struct with a reference type member (and not defining it as a class)? For example, to define this struct:
public struct SomeStruct
{
string name;
Int32 place;
}
I asking because I know that a struct is a value type, and to define in it some reference type does not make any sense.
Am I right? Can someone explain this?
Yes, it is possible, and yes, it is usually a bad practice. If you look at the . NET framework itself, you'll see virtually all structs contain primitive value types alone.
Structs are value types, while classes are reference types, and the runtime deals with the two in different ways. When a value-type instance is created, a single space in memory is allocated to store the value. Primitive types such as int, float, bool and char are also value types, and work in the same way.
A struct can be either passed/returned by value or passed/returned by reference (via a pointer) in C. The general consensus seems to be that the former can be applied to small structs without penalty in most cases.
However, unlike classes, structs are value types and do not require heap allocation. A variable of a struct type directly contains the data of the struct , whereas a variable of a class type contains a reference to the data, the latter known as an object.
Passing by reference uses a pointer to access the structure arguments. If the function writes to an element of the input structure, it overwrites the input value. Passing by value makes a copy of the input or output structure argument. To reduce memory usage and execution time, use pass by reference.
If the struct is to be used by other compilation units (. c files) , place it in the header file so you can include that header file wherever it is needed. If the struct is only used in one compilation unit (. c file), you place it in that .
Nine times out of ten, you should be creating a class rather than a structure in the first place. Structures and classes have very different semantics in C#, compared to what you might find in C++, for example. Most programmers who use a structure should have used a class, making questions like this one quite frankly irrelevant.
Here are some quick rules about when you should choose a structure over a class:
But if you've made an informed decision and are truly confident that you do, in fact, need a structure rather than a class, you need to revisit point number 2 and understand what value type semantics are. Jon Skeet's article here should go a long way towards clarifying the distinction.
Once you've done that, you should understand why defining a reference type inside of a value type (struct) is not a problem. Reference types are like pointers. The field inside of the structure doesn't store the actual type; rather, it stores a pointer (or a reference) to that type. There's nothing contradictory or wrong about declaring a struct with a field containing a reference type. It will neither "slow the object" nor will it "call GC", the two concerns you express in a comment.
Declaring a field of a reference type means there needs to be space to hold the value of the reference that is pointing to the target object. Thus it makes perfect sense to have such fields in structs.
In general, a struct should only contain a public and/or mutable field of a reference type if one of the following conditions applies:
If neither condition applies, it may be appropriate for a structure to hold a field of a reference type provided all of the following conditions apply:
In other words, an object reference which is used to encapsulate the state of an object (as opposed to merely identifying it) should only be stored in a struct field if there is no execution path via which the object to which it refers might be modified.
I'm interested to hear what more experienced coders have to say about the pros and cons of this, but my understanding is that, as a value type, a variable of type SomeStruct
would be allocated from the stack, but would contain a reference to the location on the heap containing the string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With