O'Reilly logo

C# Primer: A Practical Approach by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

2.19. The struct Value Type

The struct mechanism allows us to introduce new value types into our application. The declaration looks exactly the same as for a class, except that we use the struct keyword—for example,

public struct matrix
{
    private double[,] m_mat;
    private int       m_row;
    private int       m_col;

    // ...
}

A value type stores its data directly within the object. A matrix object, for example, directly holds the m_row and m_col integer values, as well as the handle to the two-dimensional reference type array, m_mat.

When we initialize or assign one struct object with another, a deep copy is carried out; the two objects hold the same values but remain independent, unlike the shallow-copy semantics of a reference type. This both simplifies and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required