0

Is int in C# considered to be a POD type? Isn't an int a class in C# (or a struct with methods at least)?


Edit: POD types (Plain Old Data Types)

1 Answers1

2

C# has so-called value-types. Technically, all structs and primitives are value types. So if you build your hierarchies from structs only, you have what in C++ would probably be called POD.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Just one more thing: strings are not primitives in C# (they're reference types with value type comparison semantics). So you cannot use them in this context. – Frédéric Hamidi Feb 06 '15 at 16:34
  • 1
    Correct, `System.String` and it's alias `string` are classes. That means it's reference types. You would need to actually use a char array. But I'd strongly advise against it. There are better ways. – nvoigt Feb 06 '15 at 16:36
  • My understanding of POD types is that you can copy the raw memory of an `int` for example to another `int` (because they don't allocate memory on the heap, so there is no harm). So can I do that in C# (not sure however how to copy raw memory in C#, if possible at all)? –  Feb 06 '15 at 16:59
  • @johnny92 you probably could just copy all bytes on value-types. But nobody would do so in C#. There is just no need to do so. – nvoigt Feb 06 '15 at 17:02
  • 1
    @nvoigt (char) arrays are reference types in .NET. http://stackoverflow.com/a/1533770/95976 – tofi9 Feb 06 '15 at 18:40
  • C# 'struct' and C++ 'struct' have very little to do with each other. POD types are usually implemented as structs in C++ just as a convention. C# has no natural POD type - POD usually means "just data", so there is no type or convention in C# which implies this. At least in C++, there is a convention that PODs are implemented via structs (but again, C++ structs and C# structs are orthogonal). – Dave Doknjas Aug 10 '19 at 15:41