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#)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Structure_Statements
  7. {
  8. class Program
  9. {
  10. // creating three different variables in single structure
  11. struct book
  12. {
  13. public string bookname;
  14. public int price;
  15. public string category;
  16. }
  17.  
  18. static void Main(string[] args)
  19. {
  20. //Creating two book type variable
  21. book language, database;
  22.  
  23. // Storing value in language variable
  24. Console.Write("Enter book name:\t");
  25. language.bookname = Console.ReadLine();
  26. Console.Write("Enter book price:\t");
  27. language.price = Convert.ToInt32(Console.ReadLine());
  28. Console.Write("Enter book category:\t");
  29. language.category = Console.ReadLine();
  30.  
  31. //Storing value in database variable
  32. Console.Write("\n\nEnter book name:\t");
  33. database.bookname = Console.ReadLine();
  34. Console.Write("Enter book price:\t");
  35. database.price = Convert.ToInt32(Console.ReadLine());
  36. Console.Write("Enter book category:\t");
  37. database.category = Console.ReadLine();
  38.  
  39.  
  40. //Displaying value of language variable
  41. Console.Write("\n\n===================");
  42. Console.Write("\n\t\tLanguage\n");
  43. Console.Write("===================\n\n");
  44.  
  45. Console.Write("Book Name:\t{0}", language.bookname);
  46. Console.Write("\nBook Price:\t{0}", language.price);
  47. Console.Write("\nBook Category:\t{0}", language.category);
  48.  
  49. Console.Write("\n\n==================\n");
  50. Console.Write("\t\tDatabase\n");
  51. Console.Write("====================\n\n");
  52.  
  53. Console.Write("Book Name:\t{0}", database.bookname);
  54. Console.Write("\nBook Price:\t{0}", database.price);
  55. Console.Write("\nBook Category:\t{0}", database.category);
  56.  
  57. Console.ReadLine();
  58. }
  59. }
  60. }

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.

 

Share your thought