C# Struct

C# Code Snippets C# Struct


In this code snippet, we’ll make a struct in C#. 

A struct is like a lightweight class but unlike a class(reference type) it’s value type. This can give you better performance in some cases.

You can use structs as a grouping mechanism to make a small “object” that contains multiple members instead of having them scattered around. If your struct grows too big(has too many properties/methods) you should consider making a class instead.

Note: A struct can’t inherit from another struct.

Let’s see how to make a struct in the code below.


using System;

namespace Struct
    class Program
        static void Main(string[] args)
            //A struct is kind of a "light" version of a class.
            myStruct st = new myStruct();



    struct myStruct
        public string myProperty { get; set; }

        public void myMethod()
            Console.WriteLine("From myMethod.");

Resulting output:


Leave a Reply

Your email address will not be published. Required fields are marked *

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger