In this blog post we are going to learn about Structure Data structure in C#. In C# Structure is value type which stores on Stack. It is a composite value type that contain other types. You can also create object of structure like class. In C# structure can contains fields, properties, constants, constructors, properties, Indexers, operators and even other structure types.
So enough theory. Let’s create a sample console application to learn about structure.
![structure-in-csharp structure-in-csharp](https://lh3.ggpht.com/-NSkXO58tBOs/VS1lfIw1QHI/AAAAAAAAFB4/MQ-UxZc6i-0/structure-in-csharp_thumb%25255B1%25255D.png?imgmax=800)
And following the structure, I have created with properties.
![structure-map-c# structure-map-c#](https://lh5.ggpht.com/-G6WD7Us-2GI/VS1lgjFNuYI/AAAAAAAAFCI/nLJOj5Vzfd4/structure-map-c%252523_thumb%25255B1%25255D.png?imgmax=800)
That’s it. Hope you like it. I’m going to post lot more about structure in C#.
So enough theory. Let’s create a sample console application to learn about structure.
![structure-in-csharp structure-in-csharp](https://lh3.ggpht.com/-NSkXO58tBOs/VS1lfIw1QHI/AAAAAAAAFB4/MQ-UxZc6i-0/structure-in-csharp_thumb%25255B1%25255D.png?imgmax=800)
And following the structure, I have created with properties.
struct Employee { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Designation { get; set; } }And here’s how I have used with Object initializer in c#.
class Program { static void Main(string[] args) { Employee employee = new Employee { Id=1, FirstName="Jalpesh", LastName="Vadgama", Designation="Project Manager", }; Console.WriteLine(employee.Id); Console.WriteLine(employee.FirstName); Console.WriteLine(employee.LastName); Console.WriteLine(employee.Designation); } }Here is the output of application output as expected.
![structure-map-c# structure-map-c#](https://lh5.ggpht.com/-G6WD7Us-2GI/VS1lgjFNuYI/AAAAAAAAFCI/nLJOj5Vzfd4/structure-map-c%252523_thumb%25255B1%25255D.png?imgmax=800)
That’s it. Hope you like it. I’m going to post lot more about structure in C#.
You can find complete example of this blog post at github -https://github.com/dotnetjalps/StructureCSharp
0 comments:
Post a Comment
Your feedback is very important to me. Please provide your feedback via putting comments.