• In this article we will explain to what is structure in c # or how to create a structure in c# or how to use structure in c# or Features of C# structure or declare variables in  c# Structures with example.


In C# Structure is the value type data type that can contain variables, methods, properties, events ,constants, constructors, indexers, operators and even other structure types. structure are created in stack. It simplifies the program and enhance performance of code in C# programming. The struct keyword is used for creating a structure.

The structure encapsulate small unit of related variables inside a single user-defined data type. It make better speed and memory usage and also enhances performance and clarity of your code.

For example, here is the way you can declare the Product structure:

       struct Product
            public string Name;
            public int Price;
            public string Category;        }

The following program shows the use of the structure:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
     // creating three different variable in single structure
        struct Product
            public string Name;
            public int Price;
            public string Category;
    class Program
        static void Main(string[] args)
            //Creating two Product type variable
            Product Product1, Product2;
            //Storing value in Product1 variable
            Console.Write("Enter Product name:\t");
            Product1.Name = Console.ReadLine();
            Console.Write("Enter Product price:\t");
            Product1.Price = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter Product category:\t");
            Product1.Category = Console.ReadLine();
            //Storing value in Product2 variable
            Console.Write("\n\nEnter Product name:\t");
            Product2.Name = Console.ReadLine();
            Console.Write("Enter Product price:\t");
            Product2.Price = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter Product category:\t");
            Product2.Category = Console.ReadLine();
            //Displaying value of Product1 variable
            Console.Write("\n\t\t{0}\n", Product1.Category);
            Console.Write("Product Name:\t{0}", Product1.Name);
            Console.Write("\nProduct Price:\t{0}", Product1.Price);
            Console.Write("\nProduct Category:\t{0}", Product1.Category);
            //Displaying value of Product2 variable
            Console.Write("\t\t{0}\n", Product2.Category);
            Console.Write("Product Name:\t{0}", Product2.Name);
            Console.Write("\nProduct Price:\t{0}", Product2.Price);
            Console.Write("\nProduct Category:\t{0}", Product2.Category);



  • Features of C# Structures

You have already used a simple structure named product. Structures in C# are completely different from that in standard  C or C++. The C# structures have the following features:

·           Structures can have methods, fields, indexers, properties, operator methods, and events etc.

·        Structures can have defined constructors, but not destructors. However, you cannot define a default constructor for a structure. The default constructor is automatically defined and cannot be changed.

·        Unlike classes, structures cannot inherit other structures or classes.

·        Structures cannot be used as a base for other structures or classes.

·        A structure can implement one or more interfaces.

·        Structure members cannot be specified as abstract, virtual, or protected.

·        When you create a struct object using the New operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the New operator.

·        If the New operator is not used, the fields remain unassigned and the object cannot be used until all the fields are initialized.

I hope this article will help to you.

Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like