Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

C# Tutorial

C# HOME C# Intro C# Get Started C# Syntax C# Output C# Comments C# Variables C# Data Types C# Type Casting C# User Input C# Operators C# Math C# Strings C# Booleans C# If...Else C# Switch C# While Loop C# For Loop C# Break/Continue C# Arrays

C# Methods

C# Methods C# Method Parameters C# Method Overloading C# Scope C# Recursion

C# Classes

C# OOP C# Classes/Objects C# Class Members C# Constructors C# this Keyword C# Access Modifiers C# Properties C# Static C# Inheritance C# Polymorphism C# Abstraction C# Interface C# Enums C# Structs

C# Errors

C# Errors C# Debugging C# Exceptions

C# Data Structures

C# Data Structures C# List C# Dictionary C# HashSet C# Stack C# Queue

C# Advanced

C# Files C# Date and Time C# Nullable Types C# Generics C# Lambda C# LINQ

C# Cert

C# Certificate

C# How To

C# How Tos

C# Examples

C# Examples C# Compiler C# Exercises C# Quiz C# Practice Problems C# Server C# Syllabus C# Study Plan


C# Structs


Share

Structs

A struct groups related values together, like a small class.

For example, a point on the screen has an X position and a Y position. The two values belong together, so you can put them in a struct.

To create a struct, use the struct keyword:

Example

struct Point
{
  public int X;
  public int Y;
}

class Program
{
  static void Main(string[] args)
  {
    Point p = new Point();
    p.X = 10;
    p.Y = 20;
    Console.WriteLine("X: " + p.X + ", Y: " + p.Y);
  }
}

Try it Yourself »

You create and use a struct just like an object of a class: with new, and with the dot syntax (p.X).

Like a class, a struct can have a constructor, so you can set the values in one line:

Example

struct Point
{
  public int X;
  public int Y;

  public Point(int x, int y)
  {
    X = x;
    Y = y;
  }
}

class Program
{
  static void Main(string[] args)
  {
    Point p = new Point(10, 20);
    Console.WriteLine("X: " + p.X + ", Y: " + p.Y);
  }
}

Try it Yourself »



Struct vs. Class

The big difference between a struct and a class is what happens when you assign it to another variable.

A struct is copied. The new variable gets its own copy of all the values, so a change to the copy does not change the original:

Example

struct Point
{
  public int X;
  public int Y;
}

class Program
{
  static void Main(string[] args)
  {
    Point p1 = new Point();
    p1.X = 10;
    p1.Y = 20;

    Point p2 = p1;
    p2.X = 50;

    Console.WriteLine("p1: " + p1.X + ", " + p1.Y);
    Console.WriteLine("p2: " + p2.X + ", " + p2.Y);
  }
}

Try it Yourself »

p1.X is still 10. Only the copy p2 changed to 50.

A class object is shared. Here is the same code, but Point is now a class. Both variables refer to the same object, so a change through p2 also changes p1:

Example

class Point
{
  public int X;
  public int Y;
}

class Program
{
  static void Main(string[] args)
  {
    Point p1 = new Point();
    p1.X = 10;
    p1.Y = 20;

    Point p2 = p1;
    p2.X = 50;

    Console.WriteLine("p1: " + p1.X + ", " + p1.Y);
    Console.WriteLine("p2: " + p2.X + ", " + p2.Y);
  }
}

Try it Yourself »

Now p1.X is 50 too.

This is why a struct is called a value type (the values are copied), and a class is called a reference type (the variables refer to the same object).


When to Use a Struct

Use a struct for small, simple data that belongs together, like a point (X and Y), a size (width and height) or a color (red, green and blue values).

Use a class for bigger things with more behavior, like a car or a player. If you are not sure, use a class.

StructClass
Keywordstructclass
When you assign it to another variableThe values are copiedBoth variables share the same object
Can inherit from another classNoYes
Best forSmall, simple dataBigger things with more behavior


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.