Reputation: 755
I have a question on scope.
Why can I not return d and not access c?
public string Test(string t)
{
var x = someoutcome();
//test to be sure of outcome
if(x != null)
{
var b = "A string to be returned" + t;
var c = anothermethod();
if(typeof(c) == string)
{
var d = "Hurray c and b are strings" + b;
}
}
return d;
}
Upvotes: 0
Views: 106
Reputation: 235
yes the problem is really the scope of var d
. After the }
just after var d
the variable doesn't exist anymore and you try to return it. This will rise an exception.
May be you need to declare var d
at the same level as var x
, it will be accessible in the if(x != null){...}
loop. And you need to give it a default value, because if you wan't to return d and if x == Null??? you will have to anticipate it, d will be null, you cannot return null;
you return d in the loop if(x != null){...}
and after }
you return a default value like this:
public string Test(string t)
{
var x = someoutcome();
//test to be sure of outcome
if(x != null)
{
var b = "A string to be returned" + t;
var c = anothermethod();
if(typeof(c) == string)
{
var d = "Hurray c and b are strings" + b;
return d;//d exist only in this loop, and is return, the method is stoped here
}
}
return empty.string;//if the code can't return d (x==Null or typeof(c) != string)
}
Upvotes: 0
Reputation: 7018
Block declares the scope of variable start with {
and ends with }
If you declare a variable within a block construct such as an If statement, that variable's scope is only until the end of the block. The lifetime is until the procedure ends
look at the scope of variables https://msdn.microsoft.com/en-us/library/ms973875.aspx
Upvotes: 0
Reputation: 22008
Other answers explain scope. Here is a fixed version of your code:
public string Test(string t)
{
var x = someoutcome();
// define d here to have it available for return at the end of method block
// note: you have to provide default value to return in case one of following "if" conditions are not true
string d == null;
if(x != null)
{
var b = "A string to be returned" + t;
var c = anothermethod();
if(c.GetType() == typeof(string))
d = "Hurray c and b are strings" + b;
}
return d;
}
or simply (without defining d
):
...
if(c.GetType() == typeof(string))
return "Hurray c and b are strings" + b;
}
return null;
Upvotes: 0
Reputation: 13676
Why i cannot return d
Reference to 'd' exists only in your if
block and is lost when it finishes.
Why i cannot access c?
This is whole of a different story. The typeof
operator is used to obtain the System.Type
object and expects a type as parameter and not variable or reference to an instance of a type. Your 'c' is just a variable that holds a reference to type String
. Change typeof(c)
to typeof(String)
or c.GetType()
and you'll see that it works now
Upvotes: 0
Reputation: 2604
C# Spec, Section 3.7
The scope of a local variable declared in a local-variable-declaration is the block in which the declaration occurs.
What does it mean ?
Scope of d
variable is starting from here:
if(typeof(c) == string)
{
and end with }
. This is called block
.
Your variable d
-s scope is block of mentioned if
.
Upvotes: 3
Reputation: 5747
You're accessing c
in the correct scope, I see no error here. The reason why you cannot return d
is quite obvious: it is defined in an scope which does not include return d
.
public string Test(string t)
{
var x = someoutcome();
//test to be sure of outcome
if(x != null)
{
var b = "A string to be returned" + t;
var c = anothermethod();
if(typeof(c) == string)
{
var d = "Hurray c and b are strings" + b;
} // ** FROM HERE ON, d WONT BE ACCESSIBLE ANYMORE **
} // ** FROM HERE ON, b & c WONT BE ACCESSIBLE ANYMORE **
return d;
}
But you could return d
directly instead of writing it in an variable:
public string Test(string t)
{
var x = someoutcome();
//test to be sure of outcome
if(x != null)
{
var b = "A string to be returned" + t;
var c = anothermethod();
if(typeof(c) == string)
{
return "Hurray c and b are strings" + b;
}
}
return string.Empty;
}
Please have a look at this site to read more about variables in scopes.
Upvotes: 0