struct

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
Exam Prep. Guides
Exam 70-536 Study Guide

1. Types and collections

2. Process, threading,…
3. Embedding features
4. Serialization, I/O
5. .NET Security
6. Interop., reflection,…
7. Global., drawing, text

edit

Contents


The Basics

A struct is a simple user-defined type, a lightweight alternative to a class. They support access modifiers, constructors, indexers, methods, fields, nested types, operators, and properties. They may not declare a default constructor (a constructor with no parameters) or a destructor, however you can declare constructors, but they must take parameters. Unlike classes, structures do not support compile-time initialization of instance fields, they cannot derive from anything other than System.ValueType and they cannot be the base of another class. However, they may implement an Interface. They can be instantiated without using a new operator.

Structures take up fewer resources in memory than classes, so when you have a small, frequently used class, give some thought to using a struct instead. Note though that performance can suffer when using structures in situations where reference types are expected due to boxing and unboxing.

struct members may not be declared protected.

Declaration

A struct is declared as follows:

[attributes] [modifiers] struct identifier [:interfaces]
{
  body [;]
}

The attributes is optional and is used to hold additional declarative information.

The modifier is optional. The allowed modifiers are new, static, virtual, abstract, override, and a valid combination of the four access modifiers (public, protected, internal and private).

The keyword struct must be followed by an identifier that names the struct.

The interfaces list is optional. The list contains the interfaces implemented by the struct, all separated by commas.

The body contains the member declarations.

Example 1 - A coordinate

Imagine that you had a need for a class to define a coordinate. This is a good candidate for a structure.

public struct Coords
{
  public int X, Y;
 
  public Coords(int p1, int p2)
  {
    X = p1;
    Y = p2;
  }
 
  // Override the ToString method so the value appears in text
  public override string ToString()
  {
    return String.Format("({0},{1})", X, Y);
  }
}

Which can be created in the following manner:

class Program
{
  static void Main(string[] args)
  {
    Coords coord = new Coords(3,4);
    Console.WriteLine(coord.ToString());
  }
}


Example 2 - A complex number

A complex number has both real and imaginary parts, for instance 1 + 2i. This can be implemented as a simple struct.

For information on the string formatting used in the ToString method, see C# String Theory Tips and Tricks.
public struct Complex
{
  public int real;
  public int imaginary;
 
  public Complex(int r, int i)
  {
    real = r;
    imaginary = i;
  }
 
  public double Magnitude()
  {
    return System.Math.Sqrt(real * real + imaginary * imaginary);
  }
 
  // Override the ToString method so the value appears in text
  public override string ToString()
  {   
    return (String.Format("{0}{1}i", real.ToString("#;-#;+0"), 
       imaginary.ToString("+#;-#;+0")));
  }
}

Which can be created in the following manner:

class Program
{
  static void Main(string[] args)
  {
    Complex comp = new Complex(3,4);
    Console.WriteLine(comp.ToString() + " with magnitude " + comp.Magnitude());
  }
}

Which prints

3+4i with magnitude 5

MSDN references


Previous_Page_.gif Next_Page_.gif




Personal tools