Structure (C#)

In this chapter you will learn:
  • What is structure in C#?
  • How to create user defined data types using structure?
  • How to implement structure in C# programming?

The structure is the value type data type that can contain variables, methods, properties, events and so on. It simplifies the program and enhances the performance of code in C# programming.

The structure encapsulates a small group of related variables inside a single user-defined data type. It improves speed and memory usage and also enhances performance and clarity of your code.

How to use structure in C#?

It is very simple to use a structure in C#. The following programming example will show you to how to create and use a structure in C# programming.

 
Programming Example of Structure (C#)

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

namespace Structure_Statements
{
    class Program
    {
        // creating three different variables in single structure
        struct book
        {
            public string bookname;
            public int price;
            public string category;
        }

        static void Main(string[] args)
        {
            //Creating two book type variable
            book language, database;

            // Storing value in language variable
            Console.Write("Enter book name:\t");
            language.bookname = Console.ReadLine();
            Console.Write("Enter book price:\t");
            language.price = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter book category:\t");
            language.category = Console.ReadLine();

            //Storing value in database variable
            Console.Write("\n\nEnter book name:\t");
            database.bookname = Console.ReadLine();
            Console.Write("Enter book price:\t");
            database.price = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter book category:\t");
            database.category = Console.ReadLine();


            //Displaying value of language variable
            Console.Write("\n\n===================");
            Console.Write("\n\t\tLanguage\n");
            Console.Write("===================\n\n");

            Console.Write("Book Name:\t{0}", language.bookname);
            Console.Write("\nBook Price:\t{0}", language.price);
            Console.Write("\nBook Category:\t{0}", language.category);

            Console.Write("\n\n==================\n");
            Console.Write("\t\tDatabase\n");
            Console.Write("====================\n\n");

            Console.Write("Book Name:\t{0}", database.bookname);
            Console.Write("\nBook Price:\t{0}", database.price);
            Console.Write("\nBook Category:\t{0}", database.category);

            Console.ReadLine();
        }
    }
}

Output

Enter book name :         C Sharp
Enter book price :         34
Enter book category :    Object Oriented Programming
Enter book name :         SQL Server
Enter book price :         23
Enter book category :    Database Programming

=============================================
Language
=============================================
Enter book name :         C Sharp
Enter book price :         34
Enter book category :    Object Oriented Programming

=============================================
Database
=============================================
Enter book name :         SQL Server
Enter book price :         23
Enter book category :    Database Programming
__

In the preceding example, we create a structure named book that contains three variables as bookname, price, category. These variables can be accessed by creating the object of book structure.

Next, we create two variables of book as language and database. Then store the value in language and database and display the value on console.

Summary

In this chapter you learned what structure is and how to use it in C# programming. In next chapter you will get some programming examples.

More Articles

 

Share your thought