Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structs - real life examples? [closed]

Tags:

c#

types

struct

There are any number of questions here on SO dealing with the differences between Structs and Classes in C#, and when to use one or the other. (The one sentence answer: use structs if you need value semantics.) There are plenty of guidelines out there about how to choose one or the other, most of which boil down to: use a class unless you meet these specific requirements, then use a struct.

This all makes sense to me.

However, I can't seem to find any real-life examples of people using structs in a system. I'm (semi-)new to C#, and I'm having trouble imagining a concrete situation where structs are really the right choice (at least, I haven't run into one yet.)

So, I turn to the SO world-brain. What are some cases where you actually used a struct in a system where a class wouldn't have worked?

like image 763
Electrons_Ahoy Avatar asked Aug 28 '09 20:08

Electrons_Ahoy


People also ask

Can you give a real life example where you need to implement a structure?

In Escalators, Printer spooler, Car washes queue. A circular queue is used to maintain the playing sequence of multiple players in a game. A queue can be implemented in - Linked List-based Queue, Array-based Queue, Stack-based Queue.

Where are struct used?

Structs are best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created. 5) A struct is a value type. If you assign a struct to a new variable, the new variable will contain a copy of the original.

What is the use of structure explain with example?

A structure is a collection of variables of same or different datatypes. It is useful in storing or using informations or databases. Example: An employee's record must show its salary, position, experience, etc. It all can be stored in one single variable using structures.


Video Answer


2 Answers

Well a class would still work for it, but an example I could think of is something like a Point. Assuming it is an x and y value, you could use a struct.

struct Point {     int x;     int y; } 

In my mind, I would rather have a more simple representation of a pair of integers than to define a use a class with instantiations when the actual entity does not really have much(or any) behavior.

like image 178
Gavin H Avatar answered Sep 20 '22 19:09

Gavin H


I used a struct to represent a Geolocation

struct LatLng {     public decimal Lattitude     {         get;         set;     }     public decimal Longitude     {         get;         set;     } } 

this represents a single entity, for instance I can add 2 LatLng's together or perform other operations on this single entity.

MSDN-struct

The struct type is suitable for representing lightweight objects such as Point, Rectangle, and Color. Although it is possible to represent a point as a class, a struct is more efficient in some scenarios. For example, if you declare an array of 1000 Point objects, you will allocate additional memory for referencing each object. In this case, the struct is less expensive.

Also if you look at primitive types Int32,decimal,double..etc you will notice they are all structs, which allows them to be value types whilst allowing them to implement certain crucial interfaces.

like image 34
Stan R. Avatar answered Sep 19 '22 19:09

Stan R.