Joan Venge
Joan Venge

Reputation: 331112

Why is params keyword not contextual?

The reason I ask is, it's only valid in method parameter declaration, isn't it? I was trying to make a variable inside the function body called "params", but of course this is not a big deal, just wondering the reason MS chose to make it a global keyword and not contextual.

Upvotes: 7

Views: 549

Answers (4)

Eric Lippert
Eric Lippert

Reputation: 660159

tvanoffson's answer conjectures that it would be hard to make "params" contextual. It actually wouldn't be that hard. Consider:

void M(params x)

What we'd do in this case, hypothetically, is first attempt to find a type 'params'. If we could find one, great, we're done. If we couldn't, then we have a bit of a problem. suppose for example instead of x it was

void M(params Int32)

Clearly that's an error, but what error? Should we assume that Int32 is the parameter name and give the error "you're missing the type"? Should we assume that Int32 is the type and give an error saying that the type has to be an array type, and that you're missing the identifier? Should we give an error saying that there is no type named 'params'? What is the right thing to do here? Clearly we could figure something out but is not obvious.

It's the error cases that are tricky with contextual keywords; getting the success cases working is actually pretty straightforward.

But really, it's not so much that it is hard to make contextual as that making it contextual is not a really big win. Making "set" and "value" contextual was a big win because we assumed that all kinds of people are going to be wanting to make local variables with names like "set" and "value". "params" isn't even an English word, so it seems less likely that anyone is going to want to use it. There's no big benefit to making it contextual, so the cost of the feature is not justified.

Upvotes: 7

tvanfosson
tvanfosson

Reputation: 532505

Eric Lippert has a blog post covering contextual and reserved keywords and their history. Though it doesn't explicitly explain why params is in the reserved list (it has been since 1.0), it implies that it belongs to the set of reserved words that would be hard to make contextual.

Upvotes: 4

Adam Lear
Adam Lear

Reputation: 38778

The same could be asked about any other keyword. For example, why isn't "class" contextual since it's only used in class declarations?

To me a keyword is a keyword. I imagine it greatly simplifies the lexical analysis part of compilation to not have to be THAT context aware.

As an aside, you can use the @ symbol to allow you to declare a variable called 'params' (or any other reserved keyword):

var @params = new int[] { 1, 2 };

Upvotes: 10

Flipster
Flipster

Reputation: 4401

The params keyword does look to be only useful in method parameter declaration, but I actually agree with MS that it wouldn't be a good idea to let keywords be used for instance names, anyway.

Perhaps they are reserving the ability to have something like this in C# 8.0:

params Customers = DB.GetCustomerList();

Or otherwise allow params in a local scope as well.

Upvotes: 2

Related Questions