C# Structs

Description

A struct is similar to a class, with the following key differences:

  • A struct is a value type, whereas a class is a reference type.
  • A struct does not support inheritance.

A struct can have all the members a class can, except the following:

  • A parameterless constructor
  • A finalizer
  • Virtual members

A struct is used when value-type semantics are desirable.

Because a struct is a value type, each instance does not require instantiation.

Syntax

A struct is similar to a class, but is a value type, not a reference type. Here is the general form of a struct:


struct name : interfaces {
   // member declarations
}

Example

Here is an example of declaring and calling struct constructors:


/*from w w  w  . jav  a 2s . c  o  m*/
public struct Point
{
  int x, y;
  public Point (int x, int y) { this.x = x; this.y = y; }
}

Point p1 = new Point ();       // p1.x and p1.y will be 0
Point p2 = new Point (1, 1);   // p1.x and p1.y will be 1

Example 2

A simple struct with method


using System;/*www  . j a va 2 s.c  o  m*/

struct Fraction {

  public int numerator;
  public int denominator;

  public void Print( ) {
    Console.WriteLine( "{0}/{1}", numerator, denominator );
  }
}

public class MainClass {

  public static void Main( ) {
    Fraction f;
    f.numerator   = 5;
    f.denominator = 10;
    f.Print( );

    Fraction f2 = f;
    f.Print( );

    f2.numerator = 1;
    f.Print( );
    f2.Print( );
  }
}

The code above generates the following result.

Example 3


using System;//  ww w. ja  va 2 s .co  m

public struct Square
{
   public int Width
   {
      get
      {
         return width;
      }

      set
      {
         width = value;
      }
   }

   public int Height
   {
      get
      {
         return height;
      }

      set
      {
         height = value;
      }
   }
   
   private int width;
   private int height;
}

public class MainClass
{
   static void Main()
   {
      Square sq = new Square();
      sq.Width = 1;
      sq.Height = 1;
   }
}

The code above generates the following result.

Example 4


using System;// www  .  j  av a2s .c  o m

struct Number: IComparable
{
    int value;
    
    public Number(int value)
    {
        this.value = value;
    }
    public int CompareTo(object obj2)
    {
        Number num2 = (Number) obj2;
        if (value < num2.value)
           return(-1);
        else if (value > num2.value)
           return(1);
        else
           return(0);
    }
}
class MainClass
{
    public static void Main()
    {
        Number x = new Number(3);
        Number y = new Number(4);
        
        IComparable Ic = (IComparable) x;
        Console.WriteLine("x compared to y = {0}", Ic.CompareTo(y));
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Custom Types »




C# Class
C# Struct
C# Interface
C# Inheritance
C# Namespace
C# Object
C# Delegate
C# Lambda
C# Event
C# Enum
C# Attribute
C# Generics
C# Preprocessor