I have a method which I want to call itself if a certain condition is true. Initially the method accepts one parameter whose value changes before said condition is reached.
public static void Build(string str)
{
var tree = XmlBuilder.Load();
foreach (var section tree.Sections)
{
str += section.Name;
foreach (var variable in tree.Sections[section].Variables)
{
//
}
}
if (tree.Sections[section].Sections.Count >开发者_如何学Python 0)
{
// here I want to call Build(null)
}
}
I'm not checking for a null value yet - just wanna know if this is possible first? If I say if (str==null) { }
that wouldn't work because str has a value, correct? Is there any way of checking what value was PASSED IN to the method rather?
String is a reference type, so it can be null. Be aware that:
string str = null;
str += "foo";
// Now str == "foo"
If you do something like this you can preserve the original value passed into the method:
public static void Build(string str)
{
string localStr = str;
var tree = XmlBuilder.Load();
foreach (var section tree.Sections)
{
localStr += section.Name;
foreach (var variable in tree.Sections[section].Variables)
{
//
}
}
if (tree.Sections[section].Sections.Count > 0)
{
// here I want to call Build(null)
}
}
Just save it into another variable, before changing it. .NET has no way to get the passed in value of a parameter after it has been changed.
ou could send a 'null' to that function where str = null or you could check if it is = "" or String.Empty
Yes, you can pass in null
as the parameter.
Then yo ucan check in the code if str == null
, and put the required logic.
精彩评论