Lesson 31 Learning to work with Structs in C#

Structs are a weight light alternative to C# classes. The reason for this statement requires a bit of technical discussion, but if we want to summarize, examples are made. Class instances (on memory) are stored in a tree or heap structure, while Samples made from a Structs are stored as a stack.

Structs in C#

In addition, in the Structure structure as opposed to Classes, you do not work with a reference to a structure, but You have direct access to the sample made of structs. It also means that when you have one
You pass struct to the function as a parameter, it is sent as value by call, not by call We talked about the difference between this method in the lesson of sending parameters to complete C # language functions. Direction For more information, refer to the mentioned section.

Tip: When you want to show simpler data, it is better to use the structure, especially in
When you want to quantify many copies of them. There are many examples in the .NET framework where Microsoft uses a struct structure instead of classes.

Use, such as Point, Rectangle or struct Color objects. In the first step, show you a practical example of how to define and use structures, and then about We will talk about the limitations of using them instead of classes:

 static void Main(string[] args)
 Car car;
 car = new Car("Blue");
 car = new Car("Red");
struct Car
 private string color;
 public Car(string color)
 this.color = color;
 public string Describe()
 return "This car is " + Color;
 public string Color
 get { return color; }
 set { color = value; }

If you look closely at the example, you will see that this is the same code as in the class introduction section We used C #, except that we used struct instead of class in the second part. This example to
It shows well how similar the two concepts are. But according to the explanation at the beginning of the lesson As we mentioned, how are these two concepts different?

First of all, fields in struct structures cannot be initialized, ie by text code The following code cannot be assigned to a member:

private string color = “Blue”; If you specify a constructor function for struct, you must specify all fields before exiting Constructor to be set. The struct itself has a default constructor function But if you want to manually specify the constructor function, you must specify all fields in the function definition Manufacturer, set the value. On the other hand this means that you can not make a constructor function without a parameter Specify for struct, but all constructor constructor functions must have at least one parameter.

In the code In the example above, we set a specific value for the color field, which if we did not do this, the compiler Issued an error. A struct can not be inherited from other classes or other structures (inherit) and classes can not inherit from inherit structures. Structs also support interfaces, which means that any
struct can implement its own interfaces.

About Author:

My name is Wajahat Hussain Founder of bestinformativeblog. I welcome you to the Operating System of Blog – bestinformativeblog. I Am An Blogger By Passion. Well experienced for more than 4+ years in SEO, WordPress, Blogging and how to make Passive income, Explore about me by clicking below.