C
cody
IIRC in C++ we can declare variables in if statements. Why can't we do that
in C#?
Lets see that example:
object obj = GetSomeObject();
if ((Customer cust = obj as Customer)!=null)
{
cust.DoStuff();
}
else if ((Supplier supp = obj as Supplier )!=null)
{
supp.DoSomeOtherStuff();
}
I now that that specific example could be solved better but this is just an
example to give you the idea what I mean.
The problem is that at the moment I have to write it like that:
Customer cust;
Supplier supp;
if (cust = obj as Customer)!=null)
{
cust.DoStuff();
}
else if ((supp = obj as Supplier )!=null)
{
supp.DoSomeOtherStuff();
}
this unnecessarily expands the visibility of both variables to the outer
scope.
What do you think about that idea?
in C#?
Lets see that example:
object obj = GetSomeObject();
if ((Customer cust = obj as Customer)!=null)
{
cust.DoStuff();
}
else if ((Supplier supp = obj as Supplier )!=null)
{
supp.DoSomeOtherStuff();
}
I now that that specific example could be solved better but this is just an
example to give you the idea what I mean.
The problem is that at the moment I have to write it like that:
Customer cust;
Supplier supp;
if (cust = obj as Customer)!=null)
{
cust.DoStuff();
}
else if ((supp = obj as Supplier )!=null)
{
supp.DoSomeOtherStuff();
}
this unnecessarily expands the visibility of both variables to the outer
scope.
What do you think about that idea?