3

So what I am trying to do is quite simple.

I am trying to run a nameof() on some of the primitive types as I need these constants for a certain requirement.

But when I try to do that for eg nameof(bool) it says that nameof() does not exist in the current context.

And this seems to be the case with all Synonyms?

Is there any other way to do this or am I missing something.

what I am trying to do is

public static readonly string BoolConstant= nameof(bool); 

Expected result:

BoolConstant= "bool";

Will I have to write all these constants down?

FreakyAli
  • 13,349
  • 3
  • 23
  • 63
  • 7
    Could be that your language version is old-ish. `nameof()` was not introduced till C# 6.0 I believe. – Tanveer Badar Nov 14 '19 at 07:21
  • I have the latest stable c# and that's a little obvious man. I am using nameof's all over the place in my code right now. Thanks for your reply anyway – FreakyAli Nov 14 '19 at 07:23
  • 3
    @FreakyAli: I suspect you do not get the point of `nameof`. It is to express that you want the name of some symbolic element *which can change upon refactoring*. There's no point in doing `nameof(bool)` because *there is no way to refactor `bool` to be some other keyword*. If you want the string `"bool"` then just type that string. – Eric Lippert Nov 14 '19 at 07:46
  • 2
    "as I need these constants for a certain requirement" this always sounds like an [XY problem](http://xyproblem.info/). *Why* do you need this? Maybe there's a better solution for what you *actually* need to do. – JHBonarius Nov 14 '19 at 07:49
  • 1
    @FreakyAli: No, it's not "a little obvious" - we didn't have the information that you're using nameof elsewhere, and this is exactly the error you *would* get if you were using an old version of C#. Please give a little more respect to those who are trying to help you. – Jon Skeet Nov 14 '19 at 07:51
  • Additionally, I'm confused as to why you're getting that error at all. When I try using your exact code, the error is "error CS1525: Invalid expression term 'bool'" - which isn't the same thing at all (and is much clearer about the cause). – Jon Skeet Nov 14 '19 at 07:53
  • @JonSkeet I agree, that is weird a specific error makes more sense but that is what i get – FreakyAli Nov 14 '19 at 07:54
  • 1
    @JHBonarius Basically I have a situation where I have to parse the data based on the datatype that is sent from the API as a part of the string. Now my API is not in C# so I basically sent bool from the API for boolean parsing. What i realised later on is that nameof was not working with keywords. So now i changed my API to match what i can do i.e. nameof(Boolean) – FreakyAli Nov 14 '19 at 07:57
  • Which version of the C# compiler are you using? (And is this the MS compiler, or Mono, or something else?) Can you provide a [mcve] rather than just a single line? If you change that line of code to `Boolean` instead of `bool`, in exactly the same context, does the error go away? – Jon Skeet Nov 14 '19 at 07:58
  • @JonSkeet Yes, it goes away when I do that and that is what I did actually. And I am using Xamarin Forms .Net Standard with .Net Framework 4.7.xxx so my C# compiler must be what v7.3 I suppose. – FreakyAli Nov 14 '19 at 08:03
  • @TanveerBadar Sorry if I sounded rude to you in any way that was not my intention and thanks for the reply it was really appreciatted! – FreakyAli Nov 14 '19 at 08:08
  • "so my C# compiler must be what v7.3 I suppose" - I'd be more interested in what version it *says* it is, e.g. when you build. But it's probably not worth pursuing. (If you're interested, you might want to try the same exercise with a simple console app - it's entirely possible that working with Xamarin is doing something different.) – Jon Skeet Nov 14 '19 at 08:14
  • @FreakyAli It's cool. I learned something new today according to the answers listed! – Tanveer Badar Nov 14 '19 at 08:15
  • @JonSkeet I actually did so it seems to give me the same error! Which I agree is really weird. In any case, I am going to take a look into this and get back to you soon! – FreakyAli Nov 14 '19 at 09:16

2 Answers2

6

nameof is only applicable to Type and its members and not the keywords, bool is keyword, basically bool is short for Boolean.

Try nameof(Boolean), it works.

As per MS Documentation,

nameof_expression
    : 'nameof' '(' named_entity ')'
    ;

named_entity
    : simple_name
    | named_entity_target '.' identifier type_argument_list?
    ;

named_entity_target
    : 'this'
    | 'base'
    | named_entity 
    | predefined_type 
    | qualified_alias_member
    ;

named_entity can be simple_name, and simple_name can be identifier with type argument list, nowhere in this Grammar it says that named_entity can be predefined_type, int, bool etc fall under predefined_type.

So as per this grammar,

nameof(this) is not acceptable, but nameof(this.Property) is, same way no keywords can be used inside nameof(..). I don't know the reason but it seems unnecessary and also it would make compiler more complicated to distinguish between grammar that uses keywords.

nameof itself is also a keyword

List of keywords in C# https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Akash Kava
  • 39,066
  • 20
  • 121
  • 167
3

bool is a keyword and you can use like this:

typeof(bool).Name;

or

nameof(Boolean)

both result is equal -> "Boolean"

Mahdi Asgari
  • 272
  • 1
  • 13