C# structures Edit

Rakesh Kumar Sutar | 20 January 2021 | 94

Introduction

In C#, struct is the value type data type that represents data structures. It can contain a parameterized constructor, static constructor, constants, fields, methods, properties, indexers, operators, events, and nested types. struct can be used to hold small data values that do not require inheritance, e.g. coordinate points, key-value pairs, and complex data structure.

Example

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hello_World
{
    class Program
    {
        static void Main(String[] args)
        {

            Coordinate point = new Coordinate(10, 20);

            Console.WriteLine("\t" point.x); 
            Console.WriteLine("\t" point.y);
            Console.ReadLine();
        }
    }
    struct Coordinate
    {
        public int x;
        public int y;

        public Coordinate(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
}

Output