A simple struct with method : struct « struct « C# / CSharp Tutorial






using System;

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( );
  }
}
5/10
5/10
5/10
1/10








6.1.struct
6.1.1.Structures
6.1.2.Declare a simple struct
6.1.3.A simple struct with method
6.1.4.class vs struct
6.1.5.Reference value type in a struct
6.1.6.struct with value types
6.1.7.struct with value types and ref types
6.1.8.Difference between class and struct during the reference passing