我覺得需要重新說明我的問題。獲得靜態列表<T>的最佳方法是什麼?
我的問題是。獲得我可以在整個項目中使用的相同列表的最佳方式是什麼?
我的代碼看起來像現在這樣:
public static class MessagingController
{
static List<MessagingDelivery> MessagingDeliveryList = Messaging.GetMessagingDeliveryList();
}
internal static class Messaging
{
static List<MessagingDelivery> MessagingDeliveryList;
static Messaging()
{ MessagingDeliveryList = new List<MessagingDelivery>(); }
internal static void CreateMessagingText(short reference, short number, string text)
{ MessagingDeliveryList.Add(new MessagingDelivery(reference, number, text)); }
internal static void ChangeMessagingDelivery(short reference, string status, string error)
{ MessagingDelivery.ChangeStatus(reference, status, error); }
internal static List<MessagingDelivery> GetMessagingDeliveryList()
{ return MessagingDeliveryList; }
}
老問題:
什麼是 「最佳實踐」 爲獲得static List<T>
爲什麼?
代碼1:
public static List<MessagingDelivery> messagingDeliveryList
= Messaging.GetMessagingDeliveryList();
代碼2:
static List<MessagingDelivery> messagingDeliveryList
= Messaging.GetMessagingDeliveryList();
public static List<MessagingDelivery> MessagingDeliveryList
{ get { return messagingDeliveryList; } }
我假設代碼1是最快的方法。是否有充分的理由使用Code 2?
如果您唯一需要做的就是簡單地獲取列表,那麼沒有理由使用代碼2。 – kdh
http://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property-in-c – adrianbanks
'靜態'在這裏並不重要,它更多關於差異字段和屬性之間:http://stackoverflow.com/questions/653536/difference-between-property-and-field-in-c-sharp-3-0 – MarcinJuraszek