-2

In general structs should be used as write/write blocks in code. When methods are placed in a struct the CLR boxes the struct treating it in some way, as a class (?).

So wouldn't it be better to just define a struct and then define a class with all the the methods and the struct in the class?

I'm sure there's a reason why C# allows this.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Marc HPunkt
  • 439
  • 3
  • 14
  • 3
    Have you ever done date manipulation using https://msdn.microsoft.com/en-us/library/system.datetime.add(v=vs.110).aspx ? If so, there is your answer. – mjwills Feb 22 '18 at 12:04
  • 3
    "In general structs should be used as write/write blocks in code" - I don't get what you mean by that, or what gives you that idea. – Peter B Feb 22 '18 at 12:06
  • @MarcHPunkt How would you implement `ToString` on `double` without a method? – mjwills Feb 22 '18 at 12:16
  • @mjwillis with extensions :p But I guess that gets ugly real fast – Marc HPunkt Feb 22 '18 at 12:17
  • @mjwills Hmm. Good point. So are they equivalent in terms of IL in .net? – Marc HPunkt Feb 22 '18 at 12:25
  • Do you know the difference? Does a struct with methods always get boxed? or only when the method is called? – Marc HPunkt Feb 22 '18 at 12:28
  • 1
    @MarcHPunkt - Why do you think methods cause boxing? Stucts can simply have methods, which is extremely basic, useful, well supported, always existed and designed from the start. – Kobi Feb 22 '18 at 12:31
  • I thought because a struct is just a byte array in memory. So to call methods the CLR has to make it an object no? – Marc HPunkt Feb 22 '18 at 12:35
  • 1
    @MarcHPunkt I think you will be interested in reading this: https://stackoverflow.com/q/5895458/5311735 – Evk Feb 22 '18 at 13:17
  • Possible duplicate of [Method invocation on a struct?](https://stackoverflow.com/questions/5895458/method-invocation-on-a-struct) – mjwills Feb 22 '18 at 20:02

1 Answers1

4

Structs are value types and classes are reference types. That's the major difference

Look at this:

public struct MyStruct
{
    public string Name { get; set; }
}

....

MyStruct s = new MyStruct();
s.Name = "Foo";
MyStruct s2 = s;
s2.Name = "Bah";
Console.Write("Name of MyStruct s: " + s.Name); // Foo not Bah

When you assign a struct to another variable a copy is created, so you have two instances of the struct. With classes you would modify both variables because both reference the same instance.

So that's the major difference. Apart from that structs and classes are similar. You can have methods, properties, events, etc. in both. There's nothing wrong with having methods in structs.

But since structs should not be mutable (should have readonly properties) most methods that "modify" it will return a new instance of the struct(like f.e. DateTime.AddDays).

You have these members in both:

  • Fields
  • Constants
  • Properties
  • Methods
  • Constructors
  • Events
  • Indexers
  • Operators
  • Nested Types
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Structs cannot have finalizers. "Should not be mutable" - only if they carry value semantics. But this "mutable structs are evil" topic is a bit overreacted. For example the new `ValueTuple<...>` types in C# 7 are structs, too. And they are mutable. And they have assignable public fields. Terrible, isn't it? Enumerators are often structs, too. Because mutating structs is blazingly fast (unless they are boxed into an interface such as `IEnumerator`). – György Kőszeg Feb 22 '18 at 12:39
  • @taffer: you're right, was a lack of documentation on msdn (that structs can have finalizers). – Tim Schmelter Feb 22 '18 at 12:43
  • @taffer: maybe you're right, but it's one of the [guidelines](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/choosing-between-class-and-struct) that `struct`s should be immutable. – Tim Schmelter Feb 22 '18 at 12:57
  • Yes, I love that guideline, especially the 16 bytes restriction part. :) But seriously, you are right and that was I mentioned as well by the value semantics. – György Kőszeg Feb 22 '18 at 13:03