练习C#时出现问题 [英] Problem while practicing C#

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

问题描述

实践中的问题是

创建名为People的集合类,它是person类的集合。集合中的项目应该可以通过字符串索引器访问,该字符串索引器是人名,与Person.Name属性。



我在书的后面找到了答案但是它不完整。我在这里粘贴的代码



Question in practice is
"Create collection class called People that is a collection of person class.The items in the collection should be accessible via a string indexer that is name of person,identical to the Person.Name property.

I found answer at back of book but its incomplete.That code I am pasting here

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BVCSharpPCh11
{
    public class person
    {
        private string name;
        private int age;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public int Age
        {
            get { return age; }
            set { age = value; }
        }
    }

    public class People : DictionaryBase
    {
        public void Add(person newPerson) => Dictionary.Add(newPerson.Name, newPerson);
        public void remove(string name) => Dictionary.Remove(name);
        public person this[string name]
        {
            get
            {
                return (person)Dictionary[name];
            }
            set
            {
                Dictionary[name] = value;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}





我现在应该在main函数中写什么。



我尝试过:





What should I now write in main function.

What I have tried:

public person this[string name]
       {
           get
           {
               return (person)Dictionary[name];
           }
           set
           {
               Dictionary[name] = value;
           }
       }





这个语法我完全不明白。我很有经验C ++程序员仍然难以理解



This syntax I did not understood at all.I am experienced C++ programmer still finding difficult to understand it

推荐答案

字典有两个元素:Key和Value。

每个Key都是唯一的,并且用于标识包含该值的特定条目。

例如,客户词典可以将客户名称(字符串)作为密钥和客户详细信息(地址,信用额度,余额,发票,...)作为价值。

因此,当您想要客户详细信息时,您将使用客户名称访问字典:

A Dictionary has two elements: The Key, and the Value.
Each Key is unique, and serves to identify the specific entry that contains the value.
For example, a Customer Dictionary may have the customer name (a string) as the Key and the customer details (Address, credit limit, balance, invoices, ...) as the Value.
So when you wanted the customer details, you would acess the Dictionary using teh customer name:
customer = customerDictionary["John Smith"]

您的代码是doign同样的事情:

Your code is doign teh same thing:

return (person)Dictionary[name];

获取名为name的人的详细信息,并将其转换为Pe的实例rson类。返回。

Fetch the details for the person called "name", and cast them to an instance of the Person class. return that.

Dictionary[name] = value;

为名为name的人保存Person实例



其余部分是一个索引器定义,它使你的字典工作::

Save the Person Instance for the person called "name"

The rest of it is an indexer definition which makes your dictionary work::

AccessSpecifier TypeOfValue this[TypeOfKey NameOfKeyVariable]
   { 
   Getter;   // Sets the Dictionary Entry
   Setter;   // Retrieves the Dictionary entry
   }



在你的情况下,它是一个 public 属性,它包含一个 Person Value,而Key是一个 string


In your case, it's a public property, that holds a Person Value, and the Key is a string.


感谢您的回复我理解了解释。也明白我不知道

索引器概念。

请你告诉我我应该在main函数中写什么代码。

还要广泛地告诉我什么是索引器以及它的用途是什么?这样我就可以彻底学习

了。
Thanks for your reply I understood the explanation.Also understood that I don't know
indexer concept.
Will you please tell me what code should I write inside main function.
Also just broadly tell me what is indexer and for what it is used? So that I can study
it thoroughly.


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

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