我无法摆脱错误,我也不是很了解发生了什么。
我的代码看起来像这样,并且我总是有相同的错误消息:
错误消息:无法在此范围内声明名为“ test”的本地或参数,因为该名称在封闭的本地范围内用于定义本地或参数”
static void Main(string[] args)
{
string test = String.Empty;
while (!test[1].Equals('r'))
{
var privateKey = new Key(); // generate a random private key
var publicKey = privateKey.PubKey;
var Address_testnet = publicKey.GetAddress(Network.TestNet);
string test = Convert.ToString(Address_testnet);
}
Console.WriteLine("public address is {0}", test);
}
答案 0 :(得分:2)
尝试一下:
static void Main(string[] args)
{
string test = "";
while (!test[1].Equals('r'))
{
var privateKey = new Key(); // generate a random private key
var publicKey = privateKey.PubKey;
var Address_testnet = publicKey.GetAddress(Network.TestNet);
test = Convert.ToString(Address_testnet);
}
Console.WriteLine("public address is {0}", test);
}
答案 1 :(得分:2)
您已经从test
中声明了while
变量,这就是编译器向您显示此警告的原因。某些语句,例如for
,while
,if
等具有自己的可见性范围。您可以读取和填充外部变量,但是外部代码不能使用在这些语句中声明的变量。
您可以阅读本文以获得更多了解:Variable and Method Scope in Microsoft .NET
答案 2 :(得分:0)
您已经在循环外创建了一个测试变量,并且您正在尝试在循环内创建相同的变量。尝试这样做:
static void Main(string[] args)
{
//here you wanna declare test as " r"
string test = " r";
while (!test[1].Equals('r'))
{
var privateKey = new Key(); // generate a random private key
var publicKey = privateKey.PubKey;
var Address_testnet = publicKey.GetAddress(Network.TestNet);
//Remove "string" keyword in front of test
test = Convert.ToString(Address_testnet);
}
Console.WriteLine("public address is {0}", test);
}