0

I thinking need to reformulate my question.

My questions is. What is the best way to get the same List I can use in my whole project?

My code looks like this now:

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; }
}

Old question:

What is "best practice" for get a static List<T> and why?

Code 1:

public static List<MessagingDelivery> messagingDeliveryList
    = Messaging.GetMessagingDeliveryList();

Code 2:

static List<MessagingDelivery> messagingDeliveryList
    = Messaging.GetMessagingDeliveryList();

public static List<MessagingDelivery> MessagingDeliveryList
    { get { return messagingDeliveryList; } }

I assume Code 1 is the fastest way. Is there a good reason to use Code 2?

MHP
  • 81
  • 1
  • 10
  • There's no reason to use Code 2 if the only thing you need to do is to simply get the list. – kdh Mar 26 '14 at 00:04
  • http://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property-in-c – adrianbanks Mar 26 '14 at 00:06
  • 5
    `static` doesn't matter here, It's more about difference between field and property: http://stackoverflow.com/questions/653536/difference-between-property-and-field-in-c-sharp-3-0 – MarcinJuraszek Mar 26 '14 at 00:06
  • Can you list the differences in functionality/performance between the two or cite a source that talks about these two options? If you can't then this question makes very little sense. – Jeroen Vannevel Mar 26 '14 at 00:06
  • 2
    Do you believe this will ever be the biggest performance issue in your code? – Brian Rasmussen Mar 26 '14 at 00:10
  • Public static mutable list :(... The second one at least prevents replacing whole list by external code... Consider `IEnumerable` to block unintended modification of that list... – Alexei Levenkov Mar 26 '14 at 00:16
  • @Marcin it'll probably be inlined anyway – Marc Gravell Mar 26 '14 at 00:20

2 Answers2

6

Neither. A static List<T> with a name that sounds like an actively used object (rather than, say, immutable configuration data) is not fast or slow: it is simply broken. It doesn't matter how fast broken code can run (although the faster it runs, the sooner and more often you will notice it break).

That aside, by the time the JIT has done inlining, there will rarely if ever be any appreciable difference between the 2 options shown.

Besides which: that simply isn't your bottleneck. For example, what are you going to do with the list? Search? Append? Remove - from - the - right? From the left? Fetch by index? All these things are where the actual time is spent. Not the list reference lookup.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

While the first is going to be a hair faster, I would say that the second is going to be easier to maintain in the long-run by restricting access to the accessor.

To pick a mediocre example: If, in a few weeks, you suddenly need to deal with encryption or limited access rights, you only have one place to make the change. In the first example, you'd need to search the program for places that access your list, which is a far less effective use of your time. For security, especially, it might even be dangerous, if you start dumping access tokens or keys throughout the program.

So, it depends on what you need. In production, unless the few extra cycles for an method call/return is going to be significant for the purpose (which may well be the case, for some situations), I'd go with the second.

John C
  • 1,931
  • 1
  • 22
  • 34
  • 4
    I don't think it will be faster. JIT compiler will optimize it anyway. – MarcinJuraszek Mar 26 '14 at 00:10
  • It's hard to say. Given what accessors _can_ do, it might not be worth what's essentially semantic-level optimization (of course, we could just check the CIL, but...). Either way, we're talking about maybe a dozen instructions at the absolute most. Barely noticeable. – John C Mar 26 '14 at 01:13