MOCKSTACKS
EN
Questions And Answers

More Tutorials








C# Structures

In C#, a structure represent a record and it's a value type data type. It helps you to make a single variable hold related data of various data types. The struct keyword is used for creating a structure.

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.

Each member should be define as public followed by data Type and then the element name.

Example of C# Structure

using System;

struct User {
   public string firstName;
   public string lastName;
   public int age;
};  

public class testStructure {

   public static void Main(string[] args) {
      User user1;   /* Declare user1 of type User */
      User user2;   /* Declare user2 of type User */

      /* user1 specification */
      user1.firstName = "Jhon";
      user1.lastName = "Smith"; 
      user1.age = 35;

      /* user2 specification */
      user2.firstName = "William";
      user2.lastName = "Spencer"; 
      user2.age = 53;

      /* print user1 info */
      Console.WriteLine( "User 1 First Name : {0}", user1.firstName);
      Console.WriteLine("User 1 Last Name : {0}", user1.lastName);
      Console.WriteLine("User 1 age : {0}", user1.age);

      /* print user2 info */
      Console.WriteLine( "User 2 First Name : {0}", user2.firstName);
      Console.WriteLine("User 2 Last Name : {0}", user2.lastName);
      Console.WriteLine("User 2 age : {0}", user2.age);      

      Console.ReadKey();
   }
}

Output

User 1 First Name : Jhon
User 1 Last Name : Smith
User 1 age : 35
User 2 First Name : William
User 2 Last Name : Spencer
User 2 age : 53

Conclusion

In this page (written and validated by ) you learned about C# Structures . What's Next? If you are interested in completing CS tutorial, your next topic will be learning about: CS Enum.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.