why can't a constant field be of non-built-in struct type?

This is a discussion on why can't a constant field be of non-built-in struct type? within the C# Programming forums, part of the General Programming Boards category; Code: class T { enum E { } struct S { } interface I { } delegate void D(); class ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    71

    why can't a constant field be of non-built-in struct type?

    Code:
    class T
    {
            enum E { }
            struct S { }
            interface I { }
            delegate void D();
            class C { }
    
            const E e = new E();
            //const S s = default(S);
            const I i = default(I);
            const D d = default(D);
            const C c = default(C);
    
            const int x = 10;
            const string y = "s";
    
    }
    All of the above are possible except the constant field of a type that is a struct. I can see why the non-string reference types would default to the only literal expression they can represent - null - and if I'm not mistaken the default value of a struct is an instance of the struct with all its fields set to their default value, but why is it that a const struct field is not possible? is there a reason why or is it just the way c# was written? oh and by the way, what's the idea of allowing enum types to have a default constructor?

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    The value provided to a const field must be known at compile time (hence why you can make a const reference to an object, but the only legal value it can have is null).

    A struct can have a constructor, and that means its valuation cannot be guaranteed to be known at compile time. The constructor wouldn't be executed at compile time.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    A struct can have a constructor...
    To clarify, a C# struct cannot have an explicit parameterless constructor b/c compilers do not generate default constructors for value types. So even if the compiler allowed you to provide one it would never be called which would make the constructor useless.
    Last edited by VirtualAce; 12-12-2011 at 04:50 PM.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    71
    Quote Originally Posted by VirtualAce View Post
    To clarify, a C# struct cannot have an explicit parameterless constructor b/c compilers do not generate default constructors for value types. So even if the compiler allowed you to provide one it would never be called which would make the constructor useless.
    I know that C# structs can't have explicit parameterless instance constructors but your explanation doesn't help me understand why that's the case. You say that even if the compiler allowed you to provide one it would never be called. Do you mean to say that even if I could get around the pre-compile restrictions and throw one in the compiler still wouldn't generate IL code for that constructor? can you elaborate a bit more and maybe explain why compilers won't generate default constructors for value types? is it because that's the way it is or is there a reason why that's the case?

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    I did not design the language so I do not understand the reasoning behind it. If you want more information I would purchase a book on C# or perhaps do some research on MSDN to find out why this is. What I posted is how it behaves and not the why.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct field containing size of struct
    By DL1 in forum C Programming
    Replies: 7
    Last Post: 10-10-2011, 10:37 PM
  2. Replies: 2
    Last Post: 06-07-2009, 08:49 AM
  3. Replies: 12
    Last Post: 06-29-2006, 11:49 AM
  4. Why isn't bool a built-in type?
    By joshdick in forum C++ Programming
    Replies: 14
    Last Post: 01-07-2003, 02:33 AM
  5. C# Type Checking Built In
    By Troll_King in forum C# Programming
    Replies: 0
    Last Post: 01-02-2002, 05:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21