在C#中遇到这种方法,不会清除变量
我正在尝试将当前变量保存到列表中,然后
static void SaveInvoice(string name, string brand, double tireSize, double metalPrice, double donation, List<Customer> customerList)
{
//create object
Customer newCustomer = new Customer(name, brand, tireSize, metalPrice, donation);
customerList.Add(newCustomer);
Clear();
}
static void Clear()
{
string name = "No name";
string brand = "No name";
double metalPrice = 0;
double tireSize = 0;
double donation = 0;
}
在存储以在此处列出显示后清除变量
static void DisplayAllInvoices(List<Customer> customerList)
{
Console.WriteLine("\nThe Right Speed Shop");
Console.WriteLine("*************************");
Console.WriteLine("\n{0,-15}{1,-15}{2,-15}{3,-15}{4,-15}", "Name", "Brand", "Tire", "Metal Price", "Donation");
Console.WriteLine("{0,-15}{1,-15}{2,-15}{3,-15}{4,-15}", "********", "********", "********", "************", "********");
for (int i = 0; i < customerList.Count; i++)
{
Console.WriteLine("{0,-15}{1,-15}{2,-15}{3,-15}{4,-15}", customerList[i].Name, customerList[i].Brand, customerList[i].TireSize, customerList[i].MetalPrice, customerList[i].Donation);
}
}
I am trying to save current variables into list and then clear variables
static void SaveInvoice(string name, string brand, double tireSize, double metalPrice, double donation, List<Customer> customerList)
{
//create object
Customer newCustomer = new Customer(name, brand, tireSize, metalPrice, donation);
customerList.Add(newCustomer);
Clear();
}
static void Clear()
{
string name = "No name";
string brand = "No name";
double metalPrice = 0;
double tireSize = 0;
double donation = 0;
}
After storing to list display here
static void DisplayAllInvoices(List<Customer> customerList)
{
Console.WriteLine("\nThe Right Speed Shop");
Console.WriteLine("*************************");
Console.WriteLine("\n{0,-15}{1,-15}{2,-15}{3,-15}{4,-15}", "Name", "Brand", "Tire", "Metal Price", "Donation");
Console.WriteLine("{0,-15}{1,-15}{2,-15}{3,-15}{4,-15}", "********", "********", "********", "************", "********");
for (int i = 0; i < customerList.Count; i++)
{
Console.WriteLine("{0,-15}{1,-15}{2,-15}{3,-15}{4,-15}", customerList[i].Name, customerList[i].Brand, customerList[i].TireSize, customerList[i].MetalPrice, customerList[i].Donation);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
clear()
方法中,您实际上创建了对此方法本身范围范围的新变量。换句话说,您只需为clear()
方法创建五个新变量。此外,您不能更改“ noreFerrer”>“ Noreferrer”>值类型,例如
double
函数内部(您可以,但不会在封闭范围中传播)。您可以为参考类型,就像
字符串
一样,但这不是一个好练习(在大多数情况下)。为什么需要清除这些价值观?
在我看来,您不需要
clear()
方法。你只打电话给SaveInvoice
带有新值的任何时间,它将将客户添加到列表中(鉴于定义了customerList
)。In the
Clear()
method you actually create new variables that are scoped to this method itself. In other words, you just create five new variables local to theClear()
method.Also, you cannot change the value of value types, like
double
inside a function (you can, but it will not propagate back in the enclosing scope).You can do this for reference types, like a
string
but it's not a good practice (in most cases).Why do you need to clear these values?
In my view you don't need the
Clear()
method. You just call theSaveInvoice
with new values any time you want and it will add a customer to the list (given that thecustomerList
is defined).您的代码有一些麻烦。阅读评论:
但是要小心。如果调用清晰,则清除了新的customer属性,并且此对象存储在列表中。因此,您的列表项目将清除该属性。我认为您只需要新的和添加,而无需使用清晰的方法。
Your code have some troubles. Read the comments:
But be carefull. If you invoke Clear, you clear the newCustomer properties and this object is stored in your list. So your List item will have that properties cleared. I think you only need, the new and Add, without use Clear method.