在C#中遇到这种方法,不会清除变量

发布于 2025-01-23 08:58:37 字数 1332 浏览 2 评论 0原文

我正在尝试将当前变量保存到列表中,然后

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧梦荧光笔 2025-01-30 08:58:37

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 the Clear() 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 the
SaveInvoice with new values any time you want and it will add a customer to the list (given that the customerList is defined).

走野 2025-01-30 08:58:37

您的代码有一些麻烦。阅读评论:

static void SaveInvoice(string name, string brand, double tireSize, double metalPrice, double donation, List<Customer> customerList)
{
    // You create newCustomer in this static method and store in the list. 
    // After that, you never use newCustomer. You don't need clear
    Customer newCustomer = new Customer(name, brand, tireSize, metalPrice, donation);
    customerList.Add(newCustomer);

    // To clear newCustomer, you need use newCustomer Clear method, not a static method
    newCustomer.Clear();
}

// You must remove static to work with your instance
void Clear()
{
    // Then, you can use "this" properties
    this.name = "No name";
    this.brand = "No name";
    this.metalPrice = 0;
    this.tireSize = 0;
    this.donation = 0;
}

但是要小心。如果调用清晰,则清除了新的customer属性,并且此对象存储在列表中。因此,您的列表项目将清除该属性。我认为您只需要新的和添加,而无需使用清晰的方法。

Your code have some troubles. Read the comments:

static void SaveInvoice(string name, string brand, double tireSize, double metalPrice, double donation, List<Customer> customerList)
{
    // You create newCustomer in this static method and store in the list. 
    // After that, you never use newCustomer. You don't need clear
    Customer newCustomer = new Customer(name, brand, tireSize, metalPrice, donation);
    customerList.Add(newCustomer);

    // To clear newCustomer, you need use newCustomer Clear method, not a static method
    newCustomer.Clear();
}

// You must remove static to work with your instance
void Clear()
{
    // Then, you can use "this" properties
    this.name = "No name";
    this.brand = "No name";
    this.metalPrice = 0;
    this.tireSize = 0;
    this.donation = 0;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文