1

I thought that I knew how to handle structures, since I have programmed in C for years. However, I have come across this struct definition in a C# program that I am attempting to understand. It is populated with booleans and each instance of the struct is going to be a cell in an array (not shown here). I expect that the override in line 3 is used to override a method "ToString()" in a base class.

public struct Cell
    {
        public bool occupied;

        public Cell(bool occupied) { this.occupied = occupied; }

        public override string ToString() { return occupied ? "x" : "."; }
    }

I understand the first line above. I believe that I am confused about the use of methods in structures, as I am assuming that the second and third lines in the above struct definition are methods. The second line is very confusing to me.

Thank You Tom

miner_tom
  • 177
  • 1
  • 1
  • 13
  • Instead of a Second and First, could you please specify if you mean the `ToString` or `Constructor` ? – Orel Eraki Feb 06 '16 at 17:28
  • He can't specify that, because he is about to learn that `struct`s have those as well :) – Szabolcs Dézsi Feb 06 '16 at 17:29
  • The "second line" is a constructor. The "third line" is a method. Any introductory C# tutorial covers those. – David Feb 06 '16 at 17:29
  • There are plenty of answers here already, but you should also see the [C# Programming Guide's information on structs and classes](https://msdn.microsoft.com/en-us/library/ms173109.aspx). – Preston Guillot Feb 06 '16 at 17:36

5 Answers5

2

The second line in the struct is the constructor of the struct (so yeah, it's basically a method) which takes a boolean as a parameter and assigns the value passed in to the "occupied" field.

The third line is an override of the ToString method, which is inherited by everything because it's a built-in method of the Object class, which is a superclass of every other object that exists in C#. By default, it simply outputs the fully-qualified class/struct name.

John Clifford
  • 821
  • 5
  • 10
1

The struct of C# has little to do with the struct from C. In .NET, all (for practical purposes) entities inherit from Object.

It does not matter if they are classes (reference types) or structs (value types); both can have methods, constructors, properties, attributes etc.. The only limitation is that you cannot extend a concrete value type (that is, you cannot inherit from a struct), since their memory footprint and type is predefined when unboxed. Therefore, you can think of all value types being "final".

Also, you can have constructors on structs (which is what you're seeing in the middle of your example code). Note, however, that a struct always also has an implicit "default constructor" with no arguments, which initializes the data to all binary 0.

Lucero
  • 59,176
  • 9
  • 122
  • 152
0

What exactly is your confusion? You have two good guesses about lines #2 and #3, which can be easily verified with a simple test case.

Yes, the second line is a constructor, which receives a boolean and initializes a field's value.

The third line, as you guessed, is also a method which overrides the base ToString. In this case, since there's no explicit base class, the type extends the methods found in System.Object, known colloquially in C# as object. object's implementation would simply print out the type name ("MyNamespace.Cell"), but this implementation overrides it with the contents of the boolean field.

Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63
0

Structs and classes are very similar to use in C#. So your struct can have methods and a constructor. But there are a few differences. For example: A struct is called by value, a class by reference.

To choose the right one of these options look here: https://msdn.microsoft.com/en-us/library/ms229017%28v=vs.110%29.aspx

Here the differences are explained: Structs versus classes

Community
  • 1
  • 1
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
0

Main difference between struct and class in c# is that class instances are reference types, while struct instances are value types (stored in the stack, rather than the heap).

Second line in your code is just a simple constructor. Structs may have constructor as long as they are not empty constructors. (See https://msdn.microsoft.com/en-us/library/aa288208(v=vs.71).aspx)

Third line is overriding the base Object class method ToString(). Structs may define methods, there's nothing wrong with it.

For additional information about structs, make sure to check out MSDN docs about structs