在以对象为值的 C# 哈希表中,如何 return 对象值
In C# hashtable with objects as values,how do i return the object value
方法CanVote
returns true if Age >=18.
class 的构造函数为所有属性分配默认值。
我将对象添加到哈希表中,键为人名。
我需要遍历哈希表对象以打印姓名以及此人是否可以投票。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;*/
namespace HashTable
{
class theClass
{
string name;
string dob;
int age;
public theClass(string name,int age, string dob)
{
this.name = name;
this.age = age;
this.dob=dob;
}
public static string canvote(int age)
{
if (age >= 18)
return "Can Vote";
else
return "Cnnot Vote";
}
}
public class Solution
{
public static void Main()
{
Hashtable h = new Hashtable();
theClass object1 = new theClass("John",16,"Chennai");
theClass object2 = new theClass("Smita",22, "Delhi");
theClass object3 = new theClass("Vincent",25, "Banglore");
theClass object4 = new theClass("Jothi", 10, "Banglore");
h.Add("John", object1);
h.Add("Smita", object2);
h.Add("Vincent", object3);
h.Add("Jothi", object4);
Console.WriteLine("df");
Console.WriteLine(h.canvote());
Console.ReadKey();
}
}
}
这就是你的做法:
foreach (var person in h.OfType<theClass>())
{
Console.WriteLine($"{person.name} : {theClass.canvote(person.age)}");
}
这里有两个建议。
首先,您应该使用像 Dictionary<string, theClass>
这样的类型化集合,而不是 Hashtable
。 Hashtable
基本上已弃用。泛型通常可以提高性能并减少引入类型安全错误的机会。在此处查看更多信息:https://docs.microsoft.com/en-us/dotnet/standard/collections/when-to-use-generic-collections
将您对 Hashtable
的用法替换为 Dictionary<string, theClass>
,如下所示:
var h = new Dictionary<string, theClass>();
h.Add("John", object1);
h.Add("Smita", object2);
h.Add("Vincent", object3);
h.Add("Jothi", object4);
假设您将字段 name
和 age
public:
foreach (var person in h.Values)
{
Console.WriteLine($"{person.name} : {theClass.canvote(person.age)}");
}
其次,我建议您按如下方式更改 class:
将字段转换为属性。由于字段是从 class 外部访问的,因此属性是一种更好的机制,因为它可以防止外部代码以不受控制的方式更改您的 class。
将这些属性设置为 public,因为必须从 class.
外部访问它们
使 canvote
成为实例方法(不是静态的),正如其他答案中已经建议的那样。
请注意,属性只有 getter。这意味着您的 class 现在是不可变的(也就是说,对象一旦初始化就不能更改)。如果你确实想在对象初始化后改变这些值,你可以使属性 { get; set; }
.
完整列表如下:
class theClass
{
public string name { get; }
public string dob { get; }
public int age { get; }
public theClass(string name,int age, string dob)
{
this.name = name;
this.age = age;
this.dob=dob;
}
public string canVote()
{
if (age >= 18)
return "Can Vote";
else
return "Cannot Vote";
}
}
public class Solution
{
public static void Main()
{
Dictionary<string, theClass> d = new Dictionary<string, theClass>();
theClass object1 = new theClass("John",16,"Chennai");
theClass object2 = new theClass("Smita",22, "Delhi");
theClass object3 = new theClass("Vincent",25, "Banglore");
theClass object4 = new theClass("Jothi", 10, "Banglore");
d.Add("John", object1);
d.Add("Smita", object2);
d.Add("Vincent", object3);
d.Add("Jothi", object4);
foreach (var person in d.Values)
{
Console.WriteLine($"{person.name} : {person.canVote()}");
}
Console.ReadKey();
}
}
您可以使用 foreach
循环遍历 Hashtable
。另外我建议用 public 属性:
替换静态 canvote
方法
class theClass
{
string name;
string dob;
int age;
public theClass(string name, int age, string dob)
{
this.name = name;
this.age = age;
this.dob = dob;
}
public string CanVote => age >= 18 ? "Can Vote" : "Cannot Vote";
}
public static void Main()
{
Hashtable h = new Hashtable();
theClass object1 = new theClass("John",16,"Chennai");
theClass object2 = new theClass("Smita",22, "Delhi");
theClass object3 = new theClass("Vincent",25, "Banglore");
theClass object4 = new theClass("Jothi", 10, "Banglore");
h.Add("John", object1);
h.Add("Smita", object2);
h.Add("Vincent", object3);
h.Add("Jothi", object4);
foreach(DictionaryEntry item in h){
Console.WriteLine(item.Key);
Console.WriteLine((item.Value as theClass).CanVote);
Console.WriteLine();
}
Console.ReadKey();
}
此外,最好使用强类型 Dictionary<string, theClass>
而不是 Hashtable
:
public static void Main()
{
Dictionary<string, theClass> dict = new Dictionary<string, theClass>{
{"John", new theClass("John", 16, "Chennai")},
{"Smita", new theClass("Smita", 22, "Delhi")},
{"Vincent", new theClass("Vincent",25, "Banglore")},
{"Jothi", new theClass("Jothi", 10, "Banglore")}
};
foreach(var item in dict){
Console.WriteLine(item.Key);
Console.WriteLine(item.Value.CanVote);
Console.WriteLine();
}
Console.ReadKey();
}
或者甚至 HashSet 显式实现 EquilityComparer 或覆盖 Equals
和 GetHashCode
.
首先,你应该在课堂上设置年龄 属性 public,
Hashtable h = new Hashtable();
theClass object1 = new theClass("John", 16, "Chennai");
theClass object2 = new theClass("Smita", 22, "Delhi");
theClass object3 = new theClass("Vincent", 25, "Banglore");
theClass object4 = new theClass("Jothi", 10, "Banglore");
h.Add("John", object1);
h.Add("Smita", object2);
h.Add("Vincent", object3);
h.Add("Jothi", object4);
foreach (DictionaryEntry entry in h)
{
//get the Class instance
var tClass = (theClass) entry.Value;
//call static canvote method, the age property must be public
var message = theClass.canvote(tClass.age);
Console.WriteLine("{0} => {1}", entry.Key, message);
}
方法CanVote
returns true if Age >=18.
class 的构造函数为所有属性分配默认值。
我将对象添加到哈希表中,键为人名。
我需要遍历哈希表对象以打印姓名以及此人是否可以投票。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;*/
namespace HashTable
{
class theClass
{
string name;
string dob;
int age;
public theClass(string name,int age, string dob)
{
this.name = name;
this.age = age;
this.dob=dob;
}
public static string canvote(int age)
{
if (age >= 18)
return "Can Vote";
else
return "Cnnot Vote";
}
}
public class Solution
{
public static void Main()
{
Hashtable h = new Hashtable();
theClass object1 = new theClass("John",16,"Chennai");
theClass object2 = new theClass("Smita",22, "Delhi");
theClass object3 = new theClass("Vincent",25, "Banglore");
theClass object4 = new theClass("Jothi", 10, "Banglore");
h.Add("John", object1);
h.Add("Smita", object2);
h.Add("Vincent", object3);
h.Add("Jothi", object4);
Console.WriteLine("df");
Console.WriteLine(h.canvote());
Console.ReadKey();
}
}
}
这就是你的做法:
foreach (var person in h.OfType<theClass>())
{
Console.WriteLine($"{person.name} : {theClass.canvote(person.age)}");
}
这里有两个建议。
首先,您应该使用像 Dictionary<string, theClass>
这样的类型化集合,而不是 Hashtable
。 Hashtable
基本上已弃用。泛型通常可以提高性能并减少引入类型安全错误的机会。在此处查看更多信息:https://docs.microsoft.com/en-us/dotnet/standard/collections/when-to-use-generic-collections
将您对 Hashtable
的用法替换为 Dictionary<string, theClass>
,如下所示:
var h = new Dictionary<string, theClass>();
h.Add("John", object1);
h.Add("Smita", object2);
h.Add("Vincent", object3);
h.Add("Jothi", object4);
假设您将字段 name
和 age
public:
foreach (var person in h.Values)
{
Console.WriteLine($"{person.name} : {theClass.canvote(person.age)}");
}
其次,我建议您按如下方式更改 class:
将字段转换为属性。由于字段是从 class 外部访问的,因此属性是一种更好的机制,因为它可以防止外部代码以不受控制的方式更改您的 class。
将这些属性设置为 public,因为必须从 class.
外部访问它们
使
canvote
成为实例方法(不是静态的),正如其他答案中已经建议的那样。
请注意,属性只有 getter。这意味着您的 class 现在是不可变的(也就是说,对象一旦初始化就不能更改)。如果你确实想在对象初始化后改变这些值,你可以使属性 { get; set; }
.
完整列表如下:
class theClass
{
public string name { get; }
public string dob { get; }
public int age { get; }
public theClass(string name,int age, string dob)
{
this.name = name;
this.age = age;
this.dob=dob;
}
public string canVote()
{
if (age >= 18)
return "Can Vote";
else
return "Cannot Vote";
}
}
public class Solution
{
public static void Main()
{
Dictionary<string, theClass> d = new Dictionary<string, theClass>();
theClass object1 = new theClass("John",16,"Chennai");
theClass object2 = new theClass("Smita",22, "Delhi");
theClass object3 = new theClass("Vincent",25, "Banglore");
theClass object4 = new theClass("Jothi", 10, "Banglore");
d.Add("John", object1);
d.Add("Smita", object2);
d.Add("Vincent", object3);
d.Add("Jothi", object4);
foreach (var person in d.Values)
{
Console.WriteLine($"{person.name} : {person.canVote()}");
}
Console.ReadKey();
}
}
您可以使用 foreach
循环遍历 Hashtable
。另外我建议用 public 属性:
canvote
方法
class theClass
{
string name;
string dob;
int age;
public theClass(string name, int age, string dob)
{
this.name = name;
this.age = age;
this.dob = dob;
}
public string CanVote => age >= 18 ? "Can Vote" : "Cannot Vote";
}
public static void Main()
{
Hashtable h = new Hashtable();
theClass object1 = new theClass("John",16,"Chennai");
theClass object2 = new theClass("Smita",22, "Delhi");
theClass object3 = new theClass("Vincent",25, "Banglore");
theClass object4 = new theClass("Jothi", 10, "Banglore");
h.Add("John", object1);
h.Add("Smita", object2);
h.Add("Vincent", object3);
h.Add("Jothi", object4);
foreach(DictionaryEntry item in h){
Console.WriteLine(item.Key);
Console.WriteLine((item.Value as theClass).CanVote);
Console.WriteLine();
}
Console.ReadKey();
}
此外,最好使用强类型 Dictionary<string, theClass>
而不是 Hashtable
:
public static void Main()
{
Dictionary<string, theClass> dict = new Dictionary<string, theClass>{
{"John", new theClass("John", 16, "Chennai")},
{"Smita", new theClass("Smita", 22, "Delhi")},
{"Vincent", new theClass("Vincent",25, "Banglore")},
{"Jothi", new theClass("Jothi", 10, "Banglore")}
};
foreach(var item in dict){
Console.WriteLine(item.Key);
Console.WriteLine(item.Value.CanVote);
Console.WriteLine();
}
Console.ReadKey();
}
或者甚至 HashSet 显式实现 EquilityComparer 或覆盖 Equals
和 GetHashCode
.
首先,你应该在课堂上设置年龄 属性 public,
Hashtable h = new Hashtable();
theClass object1 = new theClass("John", 16, "Chennai");
theClass object2 = new theClass("Smita", 22, "Delhi");
theClass object3 = new theClass("Vincent", 25, "Banglore");
theClass object4 = new theClass("Jothi", 10, "Banglore");
h.Add("John", object1);
h.Add("Smita", object2);
h.Add("Vincent", object3);
h.Add("Jothi", object4);
foreach (DictionaryEntry entry in h)
{
//get the Class instance
var tClass = (theClass) entry.Value;
//call static canvote method, the age property must be public
var message = theClass.canvote(tClass.age);
Console.WriteLine("{0} => {1}", entry.Key, message);
}