了解C#类 [英] Understanding C# Classes

查看:63
本文介绍了了解C#类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#相当陌生,在理解类的某些方面时遇到了麻烦.例如,我有以下代码:

I am fairly new to C# and am having trouble understanding certain aspects of classes. For instance, I have the following code:

public partial class Budget : UserControl
    {
        public Budget ()                //
        {
            InitializeComponent();      // Constructor
        }                               //
    }

    public class BudgetClass
    {    
        public BudgetClass(string category, long dollars)     // Constructor
        {                                                     // with 
            this.category = category;                         // values
            this.dollars = dollars;                           // I am 
        }                                                     // supplying.

        public string category { get; set; }       // Very confused here.
        public long dollars { get; set; }          // Not sure what this is doing??
    }

    public class ProposedBudgetCollection : Collection<BudgetClass>
    {
        public ProposedBudgetCollection()
        {
            Add(new BudgetClass("Total Personal Income", 0 )); // Adding objects
            Add(new BudgetClass("Net Salary", 5));             // with supplied
            Add(new BudgetClass("Bonuses", 5));                // data into List.
            Add(new BudgetClass("Money", 100));                //
        }
    }
}



谁能用简单的英语向我解释这里发生的事情?更重要的是,我对列表中发生的事情感到困惑.看来我正在实例化一个BudgetClass的新对象,并将要提供的值放入该对象中,然后将其添加到列表中.

我的最终目标是学习如何用来自单独窗口中文本字段的用户输入替换传递给列表的绝对数据.我可以很容易地将这些值传递给班级,但是我很难将它们添加到列表中.我觉得我需要先理解这一部分,然后才能继续.有人可以帮我吗?



Can anyone explain to me the "walkthrough" in plain English of what is going on here? More to the point, I am confused as to what is going on in the List. It seems that I am instantiating a new object of BudgetClass and placing the values that I am supplying into that object and then adding it to a list.

My ultimate goal is to learn how to replace the absolute data I am passing to the List with user input from a text field in a separate window. I can pass those values into my class easily enough, but I am having a very hard time adding them to the List. I feel that I need to understand this part before I can continue. Can anyone please help me?

推荐答案

您首先了解的是引用类型值类型.作为BudgetClass(顺便说一句,由于表面上的重言式,您不认为以包含"Class"一词的名称创建一个类以及包含"Collection"一词的收集类也不是很合理吗? )是一个类,即引用类,您的集合是引用对象的集合,并且通过这些引用访问真实"对象.因此,当您从集合中获取引用(例如,按索引)并修改对象时,您只修改引用指向的内容,而不修改引用本身.这样,您可以修改可通过集合访问的某些对象的属性,但不必修改集合本身.这就是你想要的.您将替换由相同引用指向的数据.

当然,您可以简单地添加一个全新的对象并删除一些其他对象. 删除"仅表示从集合中删除其引用并忘记"它.该对象变为无法访问,因此,最终将被垃圾收集器(GC)销毁,并回收其内存.

—SA
First thing you understand is reference types vs value types. As BudgetClass (by the way, don''t you think that creating a class under the name containing the word "Class", as well as collection class containing the word "Collection" is nor very reasonable, because of apparent tautology?) is a class, that is, a reference class, your collection is a collection of reference object, and the "real" objects are accessed through these references. So, when you obtain a reference from a collection (say, by index) and modify the object, you only modify what a reference points to, not the reference itself. This way, you modify the properties of some object accessible through the collection, but don''t modify the collection itself. This is what you want. You replace the data pointed by the same references.

Of course, you can simply add a brand new object and remove some other object. "Removing" simply means removing its reference from the collection and "forgetting" it. The object becomes unreachable and due to this, will eventually destroyed by Garbage Collector (GC), and its memory will be reclaimed.

—SA


我建​​议您通过 .NET零本书工作. a> [ ^ ]中提供了有关这些语言功能的一些出色的基本帮助.您也可以尝试 C#教程 [
I would suggest working through .NET Book Zero[^] for some excellent basic help on these features of the language. You can also try the C# Tutorials[^] provided by Microsoft.


这篇关于了解C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆