Skip to content
logo-mike
Menu
  • Home
  • ASP NET Core
  • ASP NET 5
  • ASP NET 7
  • Javascript
  • C#
  • Other
Menu
How to Use Structures

Learn How to Use Structures in C#

Posted on August 10, 2022

In C#, the two main type categories are value types (such as structures), and reference types (such as classes). Because a structure (or struct) is a value type it is data-centric.

A struct can store attributes and related methods that, preferably, do not focus on behavior.

How to Declare a Struct

Each struct declaration must have the struct keyword. Precede the struct keyword with an access modifier and include a name and a pair of curly braces after it. Here’s how to declare a struct called Game:

public struct Game {}

How to Populate a Struct

A struct stores data within its curly braces, in the form of attributes, constructors, methods, events, etc.

public struct Game
{
    public string name;
    public string description;
    public int levels;
 
    public Game (string name, string description, int levels) {
        this.name = name;
        this.description = description;
        this.levels = levels;
    }
 
    public override string ToString() => $"Name: {name}, Description: {description}, Levels: {levels}";
}

The Game struct now has three attributes, a constructor, and a method. These are three primary components of a C# struct, which are also three of the primary components of a C# class.

How to Create a Struct Variable

Each instance of a C# struct is known as a variable. There are two ways to create a variable from a struct. You can use a constructor:

Game game1 = new Game("Pokémon GO", "Lorem ipsum dolor sit amet.", 50);

Or you can assign values to individual attributes of a struct using the dot (.) operator:

Game game1;
game1.name = "Pokémon GO";
game1.description = "Lorem ipsum dolor sit amet.";
game1.levels = 50;

Both approaches achieve the same result. The code above uses C# to develop a game object called Pokémon GO that has a brief description and 50 levels. So, now you can use the game1 object.

Console.WriteLine(game1.ToString());

This prints the following output to the console:

Name: Pokémon GO, Description: Lorem ipsum dolor sit amet., Levels: 50

The Differences Between a Struct and a Class

A struct and a class have a similar appearance, but they have many notable differences. They use different keywords for declaration. And structs support neither null references nor inheritance.

READ  What to use? ORM vs Stored Procedures

Recent Posts

  • Microsoft 365 Basic – What’s in the new Office suite? Is it worth buying?
  • Office 365 vs Office 2021 – on-premises or cloud – which should you choose?
  • Microsoft Office 365 – List of releases, most important changes for the company
  • The office will become Microsoft 365
  • How to Check If the Docker Daemon or a Container Is Running
  • .NET 7 WebAssembly Plans: Mixed-Mode AOT, Multi-Threading, Web Crypto
  • Opdex Launches Stratis AMM DEX to Support GameFi Ecosystem
  • Minimal Bitcoin Miner in C#
  • Privacy Policy
  • Terms and Conditions
  • Cookies Policy
  • Contact Us