7

I found Boolean source code on http://referencesource.microsoft.com/#mscorlib/system/boolean.cs:

public struct Boolean
{
    ...
    private bool m_value;
    ...
}

why does it not throw a StackOverflowException?

Dave Zych
  • 21,581
  • 7
  • 51
  • 66
Cologler
  • 724
  • 6
  • 18
  • 1
    You should explain why you think it would. – takendarkk Sep 03 '15 at 18:35
  • 1
    @Takendarkk bool include a bool include a a bool include a a bool include a a bool include a a bool include a a bool ........ omg – Cologler Sep 03 '15 at 18:38
  • 2
    No, a Boolean include a bool. – takendarkk Sep 03 '15 at 18:39
  • @Takendarkk `bool` is just an alias for `Boolean`. – juharr Sep 03 '15 at 18:40
  • @juharr Then please explain how it doesn't do what the OP suggested it would. – takendarkk Sep 03 '15 at 18:41
  • @Takendarkk does bool and Boolean not the same thing? – Cologler Sep 03 '15 at 18:41
  • 2
    @Takendarkk If I knew I'd give an answer. I'm just saying that it isn't as simple as `Boolean` and `bool` are not the same thing. I'm looking forward to an answer. – juharr Sep 03 '15 at 18:47
  • Probably because the compiler knows about bool/Boolean. Note the if you try something similar in your code with your own struct you'll get a compilation error `Struct member 'Tester.m_value' of type 'Tester' causes a cycle in the struct layout` as opposed to a run-time StackOverflow exception. – Ed T Sep 03 '15 at 19:21
  • 1
    I understand your question, you dont mean System.Boolean is a type which CLR recognizes. & bool is a C# Primitive that C# compiler understands. you mean the classic chicken or the egg dilemma – Kapoor Sep 03 '15 at 19:22
  • Removing all `System` references and `using`s makes the compiler don't know `Boolean`, but it still knows `bool`. And when you go to its definition, it takes you to... the `Boolean` struct. :) – Andrew Sep 04 '15 at 05:38
  • Exactly the same happens with `Int32`, it contains a `internal int m_value;`. – Andrew Sep 04 '15 at 05:40

1 Answers1

4

The reason why this works is because the bool and System.Boolean types are actually different.

The primitive bool type is a built-in type that stores 1 byte.

The System.Boolean type serves as an object wrapper for the primitive type and implements the IComparable and IConvertable interfaces. This wrapper is implemented to closely represent the primitive type so they may become logically interchangeable.

As .NET Framework users that build on the Common Type System, we simply speak of them as being the same because, in our case, the C# compiler treats the "bool" keyword as an alias for the System.Boolean type that you see implemented in mscorlib.dll.

Biscuits
  • 1,767
  • 1
  • 14
  • 22
  • 1
    when I use bool type in my code, which one is actually I use? – Cologler Sep 04 '15 at 01:16
  • I'm pretty sure you're using the System.Boolean type when you don't specify the /nostdlib compiler option, referencing mscorlib.dll normally. – Biscuits Sep 04 '15 at 04:57
  • I wonder where "Go To Definition" on that `bool` would take you ... In any code it takes you to the `Boolean` definition, just like `int` takes you to `Int32`. – Andrew Sep 04 '15 at 05:19
  • That would be an intellisense thing in your IDE. I guess .NET Framework developers could get by without that feature ;) – Biscuits Sep 04 '15 at 05:23
  • Did you actually mean "1 byte" and not "bit"? Although a boolean is obviously a bit, perhaps it needs to be stored in a full byte anyway. – Andrew Sep 04 '15 at 05:24
  • No, I meant 1 whole byte. – Biscuits Sep 04 '15 at 05:26
  • 1
    @Andrew a boolean is not a bit. It's always one (or more) byte(s) and sometimes gets optimized to 4 bytes, considering 32 bit integers are processed faster than 8 bit integers. – Bauss Sep 04 '15 at 05:45
  • I know this sounds confusing. You should check out this answer to help make sense of what @Bauss is saying. http://stackoverflow.com/a/2308052/2707705 – Biscuits Sep 04 '15 at 06:05