8 Replies - 1362 Views - Last Post: 25 August 2020 - 03:53 AM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 915
  • Joined: 15-December 14

Question about structs

Posted 21 August 2020 - 10:06 PM

I get what structs are in c# (they're classes but everything inside is public). I have a dumb question I get when I use a variable type in a class I need to say which are public and private. What I dont understand is why I need to do that with a struct. Shouldn't it be a given that everything is public?

This post has been edited by albert003: 21 August 2020 - 10:06 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Question about structs

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7530
  • View blog
  • Posts: 25,321
  • Joined: 05-May 12

Re: Question about structs

Posted 21 August 2020 - 11:49 PM

View Postalbert003, on 22 August 2020 - 01:06 AM, said:

I get what structs are in c# (they're classes but everything inside is public).

That not true in C#. It is true in C++ what all members in a struct are public by default, and all members of a C++ class are private by default. In C#, all members of structs and classes are private default. Witness:
struct Car
{
    string Driver;
}

struct Boat
{
    public string Pilot;
}

class Program
{
    static void Main(string[] args)
    {
        var car = new Car();
        car.Driver = "The Stigg";    // Error CS0122: 'Car.Driver' is inaccessible due to its protection level.

        var boat = new Boat();
        boat.Pilot = "The Skipper";
    }
}



In C#, the major difference between structs and classes are that structs are value types while classes are reference types. The simplistic way to think of this is that when you declare Car or the Boat in the code above on lines 15 and 18, memory is allocated on the stack for them the same you memory would be allocated on the stack for an integer. This is different from a reference type where the only thing allocated on the stack would be a pointer.
Was This Post Helpful? 0
  • +
  • -

#3 albert003   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 915
  • Joined: 15-December 14

Re: Question about structs

Posted 22 August 2020 - 01:10 AM

So in your honest opinion when is it best to use a struct and when is it best to use a class in a program?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7530
  • View blog
  • Posts: 25,321
  • Joined: 05-May 12

Re: Question about structs

Posted 22 August 2020 - 06:23 AM

Stick with classes until you get the point when your profiler tells you that you have a speed problem due to many small allocations that get quickly discarded; or that you absolutely know that you want a type to have value semantics rather than reference semantics.
Was This Post Helpful? 3
  • +
  • -

#5 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 244
  • View blog
  • Posts: 1,305
  • Joined: 05-December 13

Re: Question about structs

Posted 22 August 2020 - 10:30 AM

Some developers say structs are stored on the stack, but that's not always on the money. Read the links below and you will understand why, and if you still have questions after that, I'd be happy to answer them for you.

This SO answer gives a good response to your question too : https://stackoverflow.com/a/203783

You should read the coverage here by Eric Lippert on his blog post : https://docs.microso...out-value-types

Here is another one which explains how one could endure an overhead with structs : https://stackoverflow.com/a/33671876
Was This Post Helpful? 2
  • +
  • -

#6 albert003   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 915
  • Joined: 15-December 14

Re: Question about structs

Posted 23 August 2020 - 09:28 PM

SkyDiver Ok Ill stick with using class

Sheepings a few questions

1. The first person answering says its storage depends on the level. I'm guessing he means its stored either on the stack or on the heap. When is it stored in the heap and when is it stored in the stack?

2. What does it mean when they say Value types are passed by value ;) and thus can't be mutated. Mutated? Mutated into what? Then it says that reference types are passed by reference and can be mutated. So structs can't be mutated but class can? What does that mean?

3. I keep thinking about pointers and how you need to know where its stored in the memory. But why is it important for a programmer to know if something in a struct or a class is stored in the stack or on the heap?
Was This Post Helpful? 0
  • +
  • -

#7 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1040
  • View blog
  • Posts: 3,999
  • Joined: 13-June 14

Re: Question about structs

Posted 24 August 2020 - 01:42 AM

View Postalbert003, on 24 August 2020 - 05:28 AM, said:

2. What does it mean when they say Value types are passed by value ;)/> and thus can't be mutated. Mutated? Mutated into what? Then it says that reference types are passed by reference and can be mutated.


Mutation just refers to changing the value. Passing by value means a copy of the value is passed to the function so any changes to it are local to the function and are not seen by the caller. Passing by reference means no copy is made so that any changes to the value in the function will be seen by the caller.

IIRC, you've done some C++. In that language, values are passed by value by default and if you want to pass by reference, you either pass a pointer, or, well, a reference.
Was This Post Helpful? 0
  • +
  • -

#8 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 244
  • View blog
  • Posts: 1,305
  • Joined: 05-December 13

Re: Question about structs

Posted 24 August 2020 - 07:06 AM

View Postalbert003, on 24 August 2020 - 04:28 AM, said:

1. The first person answering says its storage depends on the level. I'm guessing he means its stored either on the stack or on the heap. When is it stored in the heap and when is it stored in the stack?


You didn't read the link I sent you. The reply you are referring to is the topmost up-voted answer. See the reply by Jeffrey L Whitledge, which is the one with 40 up-votes (I linked directly to it). It very clearly answers this question. If you are really struggling to understand this, maybe you would be better reading up on what value types are and when datatypes become value types and where they are stored.

I am tight on time here. But reference types will store the address where the value is being stored. So, a reference type contains a pointer to another memory location that holds the actual data, and not the actual data itself.

So all data types are value types if they hold a data value within it's own allocated memory slot. That means; variables must directly contain values. Do you understand? For a much clearer elaborate answer see these interview questions on this same topic : https://dev.to/tyrrr...vs-stack-c-5aae

I would also recommend you not carry over what you've learned from C++, since its a low-level programming language, and C sharp is considered to be at the enterprise level. Otherwise you'll just just cause yourself a lot of unnecessary confusion. I tell people the same thing when coming from VB to VB.Net and VB.Net to C#.

This post has been edited by Sheepings: 24 August 2020 - 07:08 AM

Was This Post Helpful? 1
  • +
  • -

#9 kimilimes   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-August 20

Re: Question about structs

Posted 25 August 2020 - 03:53 AM

Thanks for these answers. Really helpful for me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1