Structs C# Help

So far, you have seen how classes offer a great way of encapsulating objects in your program. You have also seen how they are stored on the heap in a way that gives you much more flexibility in data lifetime, but with a slight cost in performance. This performance cost is small thanks to the optimizations of managed heaps. However, in some situations all you really need is a small data structure. In this case, a class provides more functionality than you need, and for performance reasons you will probably prefer to use a struct. Look at this example:

class Dimensions
public double Length;
public double Width

This code defines a class called Dimensions, which simply stores the length and width of some item. Perhaps you’re writing a furniture-arranging program to let people experiment with rearranging their furniture on the computer, and you want to store the dimensions of each item of furniture. It looks like you’re breaking the rules of good program design by making the fields public, but the point is that you don’t really need all the facilities of a class for this. All you have is two numbers, which you’ll find convenient to treat as a pair rather than individually. There is no need for a lot of methods, or for you to be able,to inherit from the class, and you certainly don’t want to have the .NET runtime go to the trouble of bringing in the heap with all the performance implications, just to store two doubles.

As mentioned earlier in this chapter, the only thing you need to change in the code to define a type as a struct instead of a class is to replace the keyword class with struct:

struct Dimensions
public double Length;
public double Width;

Defining functions for structs is also exactly the same as defining them for classes’.The following code demonstrates a constructor and a property for a struct:

‘st ruct; Dimensions
public double Length;
public double Width;
Dimensions(double length, double width)

public double Diagonal
return Math.Sqrt(Length”Length + Width”Width);

In many ways, you can think of structs in C# as being like scaled-down classes. They are basically the same as classes but designed more for cases where you simply want to group some data together. They differ from classes in the following ways:

  1. Structs are value types, not reference types. This means they are stored either in the stack or inline (if they are part of another object that is stored on the heap) and have the same lifetime restrictions as the simple data types.
  2.  Structs do not support inheritance.  There are some differences in the way constructors work for structs. In particular, the compiler always supplies a default no-parameter constructor, which you are not permitted to replace.
  3.  With a struct, you can specify how the fields are to be laid out in memory .

Because structs are really intended to group data items together, you’ll sometimes find that most or all of their fields are declared as public. This is, strictly speaking, contrary to the guidelines for writing .NET code – according to Microsoft, fields (other than const fields) should always be private and wrapped by public properties. However, for simple structs, many developes would nevertheless consider public fields to be acceptable programming practice

C++ developers beware ~ structs in C# are very different from classes in their implementation. This is unlike C++, in which classes and structs are virtually the same thing. The following sections look at some of these differences between structs and classes in more detail.

Structs Are Value Types

Although structs are value types, you can often treat them syntactically in the same way as classes. For example, with the definition of the Dimensions class in the previous section, you could write:

Dimensions point = new Dimensions();
point.Length = 3
point.Width = 6;

Note that because structs are value types, the new operator does not work in the same way as it does for classes and other reference types. Instead of allocating memory on the heap, the new operator simply calls the appropriate constructor, according to the parameters passed to it, initializing all fields. Indeed, for structs it is perfectly legal to write

Dimensions point;
point.Length = 3;
point.Width = 6;

If Dimensions was a class, this would produce a compilation error, because point would contain an un\nitialized reference – an address that points nowhere, so you could not start setting values to its fields. For a struct, however, the variable declaration actually allocates space on the stack for the entire struct, so it’s ready to assign values to. Note, however, that the following code would cause a compilation error, with the compiler complaining that you are using an uninitialized variable:

 Dimensions point;
Double D = point.Length;

Structs follow the same rules as any other data type – everything must be initialized before use. A struct is considered fully initialized, either when the new operator has been called against it, or when values have been individually assigned to all its fields. And of course, a struct defined as a member field of a class is initialized by being zeroed-out automatically when the containing object is initialized.

The fact that structs are value types will affect performance, though depending on how you use your struct, this can be good or bad. On the positive side, allocating memory for structs is very fast because this takes place inline or on the stack. The same goes for removing structs when they go out of scope. On the negative side, whenever you pass a struct as a parameter or assign a struct to another struct (as in A=B,where). and Bare structs), the full contents of the struct are copied, whereas for a class only the – reference is copied. This will result in a performance loss that depends on the size of the struct, emphasizing the fact that structs are really intended for small data structures. Note, however, that when passing. a struct as a parameter to a method, you can avoid this performance loss by passing it as a ref parameter – in this case, only the address in memory of the struct will be passed in, which is just as fast as passing in a class. If you do this, though, be aware that it means the called method can in principle change the value of the struct.

Structs and Inheritance

Structs are not designed for inheritance. This means that it is not possible to inherit from a struct. The only exception to this is that structs, in common with every other type in C#, derive ultimately from the class System. Obj ect. Hence, structs also have access to the methods of System. Obj ect, and it is even possible to override them in structs – an obvious example would be overriding the ToString () method. The actual inheritance chain for structs is that each struct derives from a class, System .Val~eType, which in turn derives from System. Object. ValueType does not add any new members to Obj ect, but provides implementations of some of them that are more suitable for structs. Note that you cannot supply a different base class for a struct: every struct is derived from ValueType.

Constructors for Structs

You can define constructors for structs in exactly the same way that you can for classes, except that you are not permitted to define a constructor that takes no parameters. This may seem nonsensical, and the reason is “buried in the implementation of the .NET runtime. Some rare circumstances exist in which the .NET runtime would not be able to call a custom zero-parameter constructor that you have supplied. Microsoft has therefore taken the easy way out and banned zero-parameter constructors for structs in C#..

That said, the default constructor, which initializes all fields to zero values, is always present implicitly, even if you supply other constructors that take parameters. It’s also impossible to circumvent the default constructor by supplying initial values for fields. The following code will cause a compile-time error:

Of course, if Dimensions had been declared as a class, this code would have compiled without any problems.

Incidentaly, you can supply a Close () or Dispose () method for a struct in the same way you do for a class.

Posted on October 28, 2015 in Objects and Types

Share the Story

Back to Top
Share This