为什么我的HashSet()不能与Comparer类一起使用?

时间:2014-04-21 14:49:53

标签: c#

我正在研究一个工作视频中的示例,该视频应该将部门和员工添加到

       var d = new SortedDictionary<string, List<Employee>>();

但由于某种原因,我无法使用以下代码行进行编译。

        d.Add("AA", new HashSet<Employee>(new EmployeeComparer()));

我让它为Set工作但是知道视频让我们添加了在Code示例末尾显示的类,但我会在这里再次列出它。

    public class EmployeeComparer : IEqualityComparer<Employee>
    {
        public EmployeeComparer() { }
        public bool Equals(Employee x, Employee y)
        {
            return String.Equals(x.Name, y.Name);
        }

        public int GetHashCode(Employee obj)
        {
            return obj.Name.GetHashCode();
        }
    }

我的问题是我在这三行代码中做错了什么,所以我无法编译并运行我的例子:

    1.   d.Add("AA", new HashSet<Employee>(new EmployeeComparer()));
    2.   d.Add("BB", new HashSet<Employee>(new EmployeeComparer()));
    3.   d.Add("CC", new HashSet<Employee>(new EmployeeComparer()));

代码:

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

namespace ConsoleApplication1
{
    class Program
    {
        public delegate void Del(string e);
        Del handler = DelegateMethod;


        public static void DelegateMethod(string message)
        {
            System.Console.WriteLine(message);
            System.Console.ReadKey();
        }

        public void testDel(Del d)
        {
            d.Invoke("L");
        }

        static void Main(string[] args)
        {
            Program p = new Program();
            //p.handler("Hello World");
            //p.handler("DisneyLand");
            //p.handler("Cattle Wars");
            //p.testDel(p.handler);
            var d = new SortedDictionary<string, List<Employee>>();

            d.Add("AA", new HashSet<Employee>(new EmployeeComparer())); <-Error
            d["AA"].Add(new Employee { Name = "A" });
            d["AA"].Add(new Employee { Name = "B"} );
            d["AA"].Add(new Employee { Name = "C"} );
            d["AA"].Add(new Employee { Name = "C" });
            d["AA"].Add(new Employee { Name = "C" });

            d.Add("BB", new HashSet<Employee>(new EmployeeComparer())); <- Error
            d["BB"].Add(new Employee { Name = "E"} );
            d["BB"].Add(new Employee { Name = "F"} );
            d["BB"].Add(new Employee { Name = "A"} );

            d.Add("CC", new HashSet<Employee>(new EmployeeComparer())); <- Error
            d["CC"].Add(new Employee { Name = "Z"} );
            d["CC"].Add(new Employee { Name = "X"} );
            d["CC"].Add(new Employee { Name = "Y"} );


            foreach (var a in d)
            {
                Console.WriteLine(a.Key);
                foreach (var e in a.Value)
                {
                    Console.WriteLine("\t" + e.Name);
                }
            }
            Console.ReadKey();
        }
    }

    public class EmployeeComparer : IEqualityComparer<Employee>
    {
        public EmployeeComparer() { }
        public bool Equals(Employee x, Employee y)
        {
            return String.Equals(x.Name, y.Name);
        }

        public int GetHashCode(Employee obj)
        {
            return obj.Name.GetHashCode();
        }
    }
}

班级员工:

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

namespace ConsoleApplication1
{
    class Employee
    {
        public string Name { get; set; }
    }
}

错误表:

Error   1   Inconsistent accessibility: parameter type 'ConsoleApplication1.Employee' is less accessible than method 'ConsoleApplication1.EmployeeConmparer.Equals(ConsoleApplication1.Employee, ConsoleApplication1.Employee)' C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  66  21  ConsoleApplication1
Error   2   Inconsistent accessibility: parameter type 'ConsoleApplication1.Employee' is less accessible than method 'ConsoleApplication1.EmployeeConmparer.Equals(ConsoleApplication1.Employee, ConsoleApplication1.Employee)' C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  66  21  ConsoleApplication1
Error   3   Inconsistent accessibility: parameter type 'ConsoleApplication1.Employee' is less accessible than method 'ConsoleApplication1.EmployeeConmparer.GetHashCode(ConsoleApplication1.Employee)'  C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  71  20  ConsoleApplication1
Error   4   The best overloaded method match for 'System.Collections.Generic.SortedDictionary<string,System.Collections.Generic.List<ConsoleApplication1.Employee>>.Add(string, System.Collections.Generic.List<ConsoleApplication1.Employee>)' has some invalid arguments  C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  34  13  ConsoleApplication1
Error   5   Argument 2: cannot convert from 'System.Collections.Generic.HashSet<ConsoleApplication1.Employee>' to 'System.Collections.Generic.List<ConsoleApplication1.Employee>'   C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  34  25  ConsoleApplication1
Error   6   The best overloaded method match for 'System.Collections.Generic.SortedDictionary<string,System.Collections.Generic.List<ConsoleApplication1.Employee>>.Add(string, System.Collections.Generic.List<ConsoleApplication1.Employee>)' has some invalid arguments  C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  41  13  ConsoleApplication1
Error   7   Argument 2: cannot convert from 'System.Collections.Generic.HashSet<ConsoleApplication1.Employee>' to 'System.Collections.Generic.List<ConsoleApplication1.Employee>'   C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  41  25  ConsoleApplication1
Error   8   The best overloaded method match for 'System.Collections.Generic.SortedDictionary<string,System.Collections.Generic.List<ConsoleApplication1.Employee>>.Add(string, System.Collections.Generic.List<ConsoleApplication1.Employee>)' has some invalid arguments  C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  46  13  ConsoleApplication1
Error   9   Argument 2: cannot convert from 'System.Collections.Generic.HashSet<ConsoleApplication1.Employee>' to 'System.Collections.Generic.List<ConsoleApplication1.Employee>'   C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  46  25

ConsoleApplication1

工作代码:

        class Program
        {
        public delegate void Del(string e);
        Del handler = DelegateMethod;
        Dictionary<string, List<Employee>> d = new 
                                                  Dictionary<string, List<Employee>>();

        public static void DelegateMethod(string message)
        {
            System.Console.WriteLine(message);
            System.Console.ReadKey();
        }

        public void testDel(Del d)
        {
            d.Invoke("L");
        }

        static void Main(string[] args)
        {
            Program p = new Program();
            //p.handler("Hello World");
            //p.handler("DisneyLand");
            //p.handler("Cattle Wars");
            //p.testDel(p.handler);
            var d = new SortedDictionary<string, HashSet<Employee>>();

            d.Add("AA", new HashSet<Employee>(new EmployeeComparer()));
            d["AA"].Add(new Employee { Name = "A" });
            d["AA"].Add(new Employee { Name = "B"} );
            d["AA"].Add(new Employee { Name = "C"} );
            d["AA"].Add(new Employee { Name = "C" });
            d["AA"].Add(new Employee { Name = "C" });

            d.Add("BB", new HashSet<Employee>(new EmployeeComparer()));
            d["BB"].Add(new Employee { Name = "E"} );
            d["BB"].Add(new Employee { Name = "F"} );
            d["BB"].Add(new Employee { Name = "A"} );

            d.Add("CC", new HashSet<Employee>(new EmployeeComparer()));
            d["CC"].Add(new Employee { Name = "Z"} );
            d["CC"].Add(new Employee { Name = "X"} );
            d["CC"].Add(new Employee { Name = "Y"} );

            foreach (var a in d)
            {
                Console.WriteLine(a.Key);
                foreach (var e in a.Value)
                {
                    Console.WriteLine("\t" + e.Name);
                }
            }
            Console.ReadKey();
        }
    }

1 个答案:

答案 0 :(得分:2)

您的字典值输入为List<Employee>,并且您尝试为其添加HashSet<Employee>值。就像你写的那样:

List<Employee> val = new HashSet<Employee>();

你也不希望编译。

错误与您正在使用的比较器无关。我建议您仔细研究编译器消息并尝试正确解释它们。他们通常会非常准确地告诉你什么是错的。在这种情况下,他们会这样做!