Structs are data structures are composed of several pieces of data, possibly of different types. They enable you to define your own types of variables based on this structure. For example, suppose that you want to store the route to a location from a starting point, where the route consists of a direction and a distance in miles. For simplicity you can assume that the direction is one of the compass points (such that it can be represented using the orientation enumeration from the last section) and that distance in miles can be represented as a double type.

Defining Structs

Structs are defined using the struct keyword as follows:
struct < typeName >
< memberDeclarations >

The < memberDeclarations > section contains declarations of variables (called the data members of the struct) in almost the same format as usual. Each member declaration takes the following form:

< accessibility > < type > < name > ;


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
    class Program
        struct abc
            public int a1;
            public int b1;

        static void Main(string[] args)
            abc obj;
            obj.b1 = 10;
            obj.a1 = 20;

You may also like...