C# Structs

In this chapter you will learn:

  1. What are Structs
  2. How to create a struct
  3. Example for C# Structs
  4. A simple struct with method
  5. Use property in a struct
  6. Interfaces and 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 ww . j  av a2 s.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;/*from   w  w w  . ja va2 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;/*from   w  w w  . j  a va2s. c o 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;//from www.ja v a 2 s  .  c  om

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.

Next chapter...

What you will learn in the next chapter:

  1. What are the difference between struct and class
  2. struct with value types and ref types
Home »
  C# Tutorial »
    C# Types »
      C# Struct
C# Structs
C# Struct Instances vs. Class Instances
C# struct Constructor
C# struct equality
C# Generic struct