今回は閉じる前にコードを調べてください。C#ConcurrentDictonaryキー以外のコレクションの値を設定する
以下のコードは動作しますが、非常にハッキングされているようですが、私はよりクリーンなコードで同じことを達成するための提案を探しています。
追加と削除を呼び出すコードは、コードに同時にアクセスする可能性のある別のスレッドからのものであるため、スレッドセーフな状態を維持する必要があります。
using System;
using System.Collections.Concurrent;
namespace Server
{
public class Company
{
public string Name { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public ConcurrentDictionary<string, Employee> Employees = new ConcurrentDictionary<string, Employee>();
}
public class Employee
{
public string First { get; set; }
public string Last { get; set; }
public string Ext { get; set; }
}
public class Clients
{
public ConcurrentDictionary<string, Company> CompaniesDict = new ConcurrentDictionary<string, Company>();
public bool Add_Company(string ID, string Name, string Address, string Phone) //This function works
{
Company MyCompany = new Company();
Employee MyEmployees = new Employee();
MyCompany.Name = Name;
MyCompany.Address = Address;
MyCompany.Phone = Phone;
MyCompany.Employees = MyEmployees;
return CompaniesDict.TryAdd(ID, MyCompany);
}
public bool Remove_Company(string ID) //This function works
{
return CompaniesDict.TryRemove(ID, Company tCompany);
}
//This is were I need the help this seems so hacked. Im not trying to update the key, but the value intstead
public bool Set_CompanyName(string ID, string Name)
{
CompaniesDict.TryGetValue(ID, out Company oCompany);
Company nCompany;
nCompany = oCompany;
nCompany.Name = Name;
return CompaniesDict.TryUpdate(ID, nCompany, oCompany);
}
public string Get_CompanyName(string ID)
{
CompaniesDict.TryGetValue(ID, out Company tCompany);
return tCompany.Name;
}
}
}
これを閉じて、重複と呼ばれるいくつかの無駄なコードにリンクしてください。申し訳ありませんがとても鈍いですが、これは最近、このサイトの仲間のコーダーによって私に起こりました。私が答えることができるような質問があれば、私に全面的に助けてもらえますか?
ご協力いただきありがとうございます。
'out'パラメータとして上書きされるオブジェクトを作成しないでください。 – Dragonthoughts
私は上記のコードでそれを修正したと信じています。私はまだC#で非常に新しいです# –
私の答えが役に立ったら、そのようにマークしてください。 – Dragonthoughts