C# Enums and Structs
ExampleC#
using System;
enum Level { Low, Medium = 5, High }
struct Point
{
public int X;
public int Y;
public Point(int x, int y)
{
X = x;
Y = y;
}
public override string ToString()
{
return "(" + X + ", " + Y + ")";
}
}
class Program
{
static void Main()
{
Level l = Level.High;
Console.WriteLine(l);
Console.WriteLine((int)l);
Console.WriteLine((int)Level.Low);
Point a = new Point(1, 2);
Point b = a;
b.X = 99;
Console.WriteLine(a);
Console.WriteLine(b);
}
}Output
High 6 0 (1, 2) (99, 2)
A struct is a value type: assigning it copies the data, so changing b did not change a. A class would share one object instead.
Quick check
What happens when you assign one struct variable to another?
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.