Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Readonly struct vs classes

Tags:

c#

.net

Assuming you only have immutable types and you have all your code up to date to C# 7.3 and your methods are using the in keyword for inputs

Why would you ever use a class instead of a readonly struct?

The downside of using structs was that copying was expensive, but assuming you prevent any copy (defensive compiler copy or expressed by code), readonly structs allow you to only copy the reference (as classes do) and avoid heap allocation and pressure on the garbage collector.

Excluding special cases (which I guess it could be a very large object that won't fit on the stack) would you use readonly struct as first choice normally?

The case I am interested in is where they are used as data containers.

like image 895
user4388177 Avatar asked Aug 14 '18 08:08

user4388177


People also ask

What is a readonly struct?

The readonly keyword is a C# modifier used to limit access to all the data members of a struct. If the readonly modifier is used in the declaration of a struct, then: The members of the struct are read-only. None of the members can have setters. A parameterized constructor is used to initialize the data members.

What is difference between struct and class?

A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member function which defines actions) into a single unit. A structure is a collection of variables of different data types under a single unit.

When should I use a struct instead of a class?

In classes, two variables can contain the reference of the same object and any operation on one variable can affect another variable. In this way, struct should be used only when you are sure that, It logically represents a single value, like primitive types (int, double, etc.). It is immutable.

Can a class be readonly?

The readonly keyword is a modifier that can be used in four contexts: In a field declaration, readonly indicates that assignment to the field can only occur as part of the declaration or in a constructor in the same class.


1 Answers

structs should not be looked on as "cheap objects"; they have similar feature sets that are overlapping in some areas and disjoint in others. For example:

  • structs can't participate in polymorphism
  • you can't treat a struct as an instance of an interface without boxing it (caveat: "constrained call", but that only works in some scenarios)
  • a lot of library APIs won't work well (or possibly at all) with structs - they expect mutable POCOs; you'll probably want to use a library to get data from a database, serialize it, or render it in a UI - all of these things choke a bit with structs
  • structs don't work well with some patterns, such as tree or sibling relationships (a Foo can't contain a Foo if it is a struct) - there are others
  • structs, and immutable types generally, can be awkward to work with

Also, note that until very recently ("ref returns" and "ref locals") it was very hard to achieve some parts of "readonly structs allow you to only copy the reference"; this is now much simpler.

But frankly, in most scenarios POCOs are just easier to work with, and are fine for most application-code scenarios.

There are certainly times when structs are an amazing choice. It just isn't every time. I would however, support the notion that if you're going to use a struct, it should be either a readonly struct (by default) or a ref struct (if you know why you're doing it); mutable non-ref structs are a recipe for pain.

like image 103
Marc Gravell Avatar answered Oct 27 '22 15:10

Marc Gravell