julian bechtold
julian bechtold

Reputation: 2271

Does a struct make sense to return Data from a function in a structured way? (when is a struct apropriate?)

Intro

So far I am trying to wrap my head around structs. I have found many answers on the Topic "When to use a struct". Most of them are vague such as advice against the use of structs in general, with few exceptions. There are explanations of when to use structs (few examples):

But no example code as to when this case actually happens that it makes sense.

probably one of the most well known Questions/Answers is this one: When should I use a struct rather than a class in C#?

Project structure

class StorageBlock<Type>:

/// this is a storage block. It is not a struct because the data is not immutable and it
/// should be passed by reference
public class StorageBlock<Type>
{
    Type[] Data;
    /* other stuff */
}

methodBlockSearcher():

public (StorageBlock<Type> FoundBlock, int FoundIndex) SearchBlock(Type searchForThisElement)
{
    StorageBlock<Type> found = Blocks.Find(searchForThisElement);
    int index = Array.IndexOf(found.Data, searchForThisElement);
    return (found,index);
} 

caller example:

(StorageBlock<Type> FoundBlock, int FoundIndex) searchResult = SearchBlock(searchElement);
/* process SearchResult(s) */

Questions

I wonder if it makes sense to convert (StorageBlock<Type> FoundBlock, int FoundIndex) searchResult to a struct. The search result should be immutable. It is only there to provide a return from specific indexing operations.

something like this:

struct BlockIndex
{
    StorageBlock<Type> Block;
    int Index;
}

Because a struct is a DataType, not a reference type, I also wonder if BlockIndex.Block (which is a class) will be a reference to the instance of block or a copy of the found block.

Upvotes: 0

Views: 101

Answers (1)

D Stanley
D Stanley

Reputation: 152634

It seems like you're comparing structs to tuples (which are compiled to ValueTuple, not Tuple), not structs to classes (which is the more common question and the context of the original suggestions). Defining tuples with that syntax is a more recent construct in C# so I don't know that there is a best practice yet on whether to explicitly define a struct or use a tuple.

Behind-the-scenes, tuples are implemented as ValueTuple structs, so there should be minimal (if any) performance difference by defining a struct. It's more for coding convenience than performance.

As a pragmatist, I would say to continue using tuples until you have a measurable problem.

The search result should be immutable

This is the only thing I see that would make me consider defining a readonly struct, since ValueTuple is not immutable by default.

Because a struct is a [value type], not a reference type, I also wonder if BlockIndex.Block (which is a class) will be a reference to the instance of block or a copy of the found block.

It will still be a reference. Reference types do not cease to be reference types just because they're a property of a struct. The value of the reference will be carried along with the struct just like an integer would.

Upvotes: 3

Related Questions