1

Why won't this work?

public static int[] GetListOfAllDaysForMonths()
{
    static int[] MonthDays = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};

    return MonthDays;
}

I had to move the variable declaration outside of the method:

    static int[] MonthDays = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
public static int[] GetListOfAllDaysForMonths()
{
    return MonthDays;
}

Also, so by creating it this way, we only have one instance of this array floating around in memory? This method sits inside a static class.

2
  • so now here's my question. Since you can't add static locals to a method, on the 2nd example, is this really saving me memory allocation? How? When is that local variable that lives outside the method called and creates that int in memory? And is it only doing that one time if this is in a static class and I use that class to call the method in example #2? Commented Oct 29, 2009 at 20:23
  • Yes, it will save the allocation. But a much more important optimization would be return an IEnumerable<int> (Enumerable.Range(1,31)) Commented Oct 29, 2009 at 20:35

4 Answers 4

17

C# doesn't support static locals at all. Anything static needs to be a member of a type or a type itself (ie, static class).

Btw, VB.Net does have support for static locals, but it's accomplished by re-writing your code at compile time to move the variable to the type level (and lock the initial assignment with the Monitor class for basic thread safety).

[post-accept addendum]
Personally, your code sample looks meaningless to me unless you tie it to a real month. I'd do something like this:

public static IEnumerable<DateTime> GetDaysInMonth(DateTime d)
{
    d = new DateTime(d.Year, d.Month, 1);
    return Enumerable.Range(0, DateTime.DaysInMonth(d.Year, d.Month))
             .Select(i => d.AddDays(i) );
}

Note also that I'm not using an array. Arrays should be avoided in .Net, unless you really know why you're using an array instead of something else.

Sign up to request clarification or add additional context in comments.

6 Comments

What would a static local even mean? I don't doubt your answer, I'm just curious since it seems like a non-concept to me.
@jprete: In some languages, you can have a static variable defined in the scope of a single method. It's static, but only usable from that method's scope.
Ah, I remember learning what the Static keyword in VB did... and subsequently the next time one of my fellow developers looked over at my code, and he blurted, "What the heck is that?"
@jprete: It sounds like you have an idea that 'static' is something like the exact opposite of local. That's not really true, though; the terms are orthogonal. "Static" means associated with the type rather than with an instance, and "local" here means in the namespace of a method rather than the namespace of a type.
[cont]... so you could easily have a variable that associated with a type (and not an instance) that's in the namespace of a particular method rather than the namespace of the whole type.
|
8

You can only create static variables in the class/struct scope. They are static, meaning they are defined on the type (not the method).

This is how C# uses the term "static", which is different from how "static" is used in some other languages.

5 Comments

Wow, this was close. Mine answer shows up first sorted by "oldest", but your answer was already here the first page view after submitting mine, so it must have come in during the split second between when mine went in the db and before the answer list was retrieved to render.
Hehehe. Didn't realize it was a race ;)
It's not, but in >2500 answers I hadn't seen that happen before.
Ok, but again this variable is defined inside a static class. So is it really a single instance of the int[] array? So if I call that method multiple times am I really saving myself from creating an allocation for the array each time?
@coffeeaddict: Yes. you are saving the reallocation.
0

What would be the scope of that static variable declared within the method? I don't think CLR supports method static variables, does it?

Comments

0

Well, apart from it not being supported, why would you want to? If you need to define something inside a method, it has local scope, and clearly doesn't need anything more than that.

4 Comments

Lots of good reasons to do this. A simple example is a method to return a random value. When working with prng's you generally want to keep the same instance around a while. You could do this by polluting the type's private namespace with a field for the prng object, but a static member that's tied to the method might make more sense
My boss wanted me to change it and said doing it this way, we aren't creating an allocation every time we need to get that list of numbers.
Prior to this code this method was creating a generic list of ints, using a foreach through it and adding 1-31 to the list. He said by me doing it that way, I was creating a new instance/allocation for that list every time. And by doing it his way (this way) I'm not.
Your boss is right, though the difference is miniscule. Small enough that personally I'd take a datetime value in as a parameter and use Enumerable.Range() to return number of days in that particular month.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.