I'm creating an anonymous method to generate a radom string.
Before declaring the method, I didn't declare any parameter with the name id
.
But I couldn't still define string id = string.Empty;
inside the method.
// No parameter within name `id` here...
Func<Task<string>> getIdAsync = null;
getIdAsync = async () =>
{
// string id = string.Empty; // error here. try to use `_id` instead of `id`
string _id = string.Empty;
// logic...
return _id;
};
// _id = ""; // we cann't re-use `_id` here, but `_id` can use `id` below
// `id` is declared after the method
string id = await getIdAsync();
Maybe I misunderstood but I think it should throw me the error when (only in this case):
// declare `id` before
string id = string.Empty;
Func<Task<string>> getIdAsync = null;
getIdAsync = async () =>
{
/*
* A local or parameter named 'id' cannot be declared in this scope
* because that name is used in an enclosing local scope to define a local or parameter
*/
string id = string.Empty;
// logic...
};
id = await getIdAsync();
Can anyone correct me?