{"id":302,"date":"2020-10-14T15:45:29","date_gmt":"2020-10-14T10:15:29","guid":{"rendered":"http:\/\/sumitjangid.com\/?p=302"},"modified":"2020-10-14T15:45:44","modified_gmt":"2020-10-14T10:15:44","slug":"structs-and-enums","status":"publish","type":"post","link":"http:\/\/sumitjangid.com\/index.php\/2020\/10\/14\/structs-and-enums\/","title":{"rendered":"Structs and Enums"},"content":{"rendered":"\n<h2>Structs<\/h2>\n\n\n\n<p>In C#, a structure is a value type data type. It helps you to make a single variable hold related data of various data types. The&nbsp;<strong>struct<\/strong>&nbsp;keyword is used for creating a structure.<\/p>\n\n\n\n<p>Structures are used to represent a record. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book \u2212<\/p>\n\n\n\n<ul><li>Title<\/li><li>Author<\/li><li>Subject<\/li><li>Book ID<\/li><\/ul>\n\n\n\n<h2>Defining a Structure<\/h2>\n\n\n\n<p>To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member for your program.<\/p>\n\n\n\n<p>For example, here is the way you can declare the Book structure \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\n\nstruct Books {\n   public string title;\n   public string author;\n   public string subject;\n   public int book_id;\n};  \n\npublic class testStructure {\n   public static void Main(string&#91;] args) {\n      Books Book1;   \/* Declare Book1 of type Book *\/\n      Books Book2;   \/* Declare Book2 of type Book *\/\n\n      \/* book 1 specification *\/\n      Book1.title = \"C Programming\";\n      Book1.author = \"Nuha Ali\"; \n      Book1.subject = \"C Programming Tutorial\";\n      Book1.book_id = 6495407;\n\n      \/* book 2 specification *\/\n      Book2.title = \"Telecom Billing\";\n      Book2.author = \"Zara Ali\";\n      Book2.subject =  \"Telecom Billing Tutorial\";\n      Book2.book_id = 6495700;\n\n      \/* print Book1 info *\/\n      Console.WriteLine( \"Book 1 title : {0}\", Book1.title);\n      Console.WriteLine(\"Book 1 author : {0}\", Book1.author);\n      Console.WriteLine(\"Book 1 subject : {0}\", Book1.subject);\n      Console.WriteLine(\"Book 1 book_id :{0}\", Book1.book_id);\n\n      \/* print Book2 info *\/\n      Console.WriteLine(\"Book 2 title : {0}\", Book2.title);\n      Console.WriteLine(\"Book 2 author : {0}\", Book2.author);\n      Console.WriteLine(\"Book 2 subject : {0}\", Book2.subject);\n      Console.WriteLine(\"Book 2 book_id : {0}\", Book2.book_id);       \n\n      Console.ReadKey();\n   }\n}<\/code><\/pre>\n\n\n\n<h2>Features of C# Structures<\/h2>\n\n\n\n<p>You have already used a simple structure named Books. Structures in C# are quite different from that in traditional C or C++. The C# structures have the following features \u2212<\/p>\n\n\n\n<ul><li>Structures can have methods, fields, indexers, properties, operator methods, and events.<\/li><li>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.<\/li><li>Unlike classes, structures cannot inherit other structures or classes.<\/li><li>Structures cannot be used as a base for other structures or classes.<\/li><li>A structure can implement one or more interfaces.<\/li><li>Structure members cannot be specified as abstract, virtual, or protected.<\/li><li>When you create a struct object using the&nbsp;<strong>New<\/strong>&nbsp;operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the New operator.<\/li><li>If the New operator is not used, the fields remain unassigned and the object cannot be used until all the fields are initialized.<\/li><\/ul>\n\n\n\n<h2>Class versus Structure<\/h2>\n\n\n\n<p>Classes and Structures have the following basic differences \u2212<\/p>\n\n\n\n<ul><li>classes are reference types and structs are value types<\/li><li>structures do not support inheritance<\/li><li>structures cannot have default constructor<\/li><\/ul>\n\n\n\n<h2>Enums<\/h2>\n\n\n\n<p>An&nbsp;<code>enum<\/code>&nbsp;is a special &#8220;class&#8221; that represents a group of&nbsp;<strong>constants<\/strong>&nbsp;(unchangeable\/read-only variables).<\/p>\n\n\n\n<p>To create an&nbsp;<code>enum<\/code>, use the&nbsp;<code>enum<\/code>&nbsp;keyword (instead of class or interface), and separate the enum items with a comma:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>enum Level\n  {\n    Low,\n    Medium,\n    High\n  }\n  class Program\n  {\n    static void Main(string&#91;] args)\n    {\n      Level myVar = Level.Medium;\n      Console.WriteLine(myVar);\n    }\n  }\n\nOutput: Medium<\/code><\/pre>\n\n\n\n<p>You cannot define a method inside the definition of an enumeration type. To add functionality to an enumeration type, create an&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/extension-methods\">extension method<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Structs In C#, a structure is a value type data type. It helps you to make a single variable hold related data of various data types. The&nbsp;struct&nbsp;keyword is used for creating a structure. Structures are&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,4,2],"tags":[],"_links":{"self":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/302"}],"collection":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/comments?post=302"}],"version-history":[{"count":2,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/302\/revisions"}],"predecessor-version":[{"id":304,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/302\/revisions\/304"}],"wp:attachment":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/media?parent=302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/categories?post=302"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/tags?post=302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}