Hacker News new | ask | show | jobs
by jmkni 2555 days ago
> ...because of how in modern OO languages like Java or C#, there is no syntax or popular naming convention to tell the difference between a data structure and an Object.

In C#, you create a class when it's an object, and a struct when it's a data structure, or am I missing something?

ie:

  // Data Structure
  public struct Foo
  {
      public string Bar { get; set; }
  }
    
  // Object
  public class Bar
  {
      public string Foo { get; set; }
  }
You can check to see if something is a Data Structure like so:

  typeof(Foo).IsValueType (true)
  typeof(Bar).IsValueType (false)
1 comments

> In C#, you create a class when it's an object, and a struct when it's a data structure, or am I missing something?

Yes and no. Due to the technical limitation that (unlike in C++[0]) structs in C# cannot derive from other structs, DTOs in C# are often implemented (or rather: generated) as classes to allow derivation from a base class that has certain behavioral hooks (say, for misc. custom serializers).

[0] See https://www.fluentcpp.com/2017/06/13/the-real-difference-bet... (the real technical difference between struct and class in C++ is just that the default visibility modifier for a struct is public and the default visibility modifier for a class is private; both can use inheritance and have virtual methods)

Cool thanks