Click here to Skip to main content
15,902,032 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Marc Codes a Bug Pin
Marc Clifton20-Sep-17 7:44
mvaMarc Clifton20-Sep-17 7:44 
GeneralRe: Marc Codes a Bug Pin
S Douglas20-Sep-17 8:40
professionalS Douglas20-Sep-17 8:40 
AnswerEasy fix, Marc Pin
Nish Nishant20-Sep-17 8:11
sitebuilderNish Nishant20-Sep-17 8:11 
GeneralRe: Easy fix, Marc Pin
Marc Clifton20-Sep-17 8:16
mvaMarc Clifton20-Sep-17 8:16 
GeneralRe: Easy fix, Marc Pin
Nish Nishant20-Sep-17 8:21
sitebuilderNish Nishant20-Sep-17 8:21 
GeneralRe: Easy fix, Marc Pin
Marc Clifton20-Sep-17 8:30
mvaMarc Clifton20-Sep-17 8:30 
GeneralRe: Easy fix, Marc Pin
Nish Nishant20-Sep-17 8:36
sitebuilderNish Nishant20-Sep-17 8:36 
GeneralRe: Marc Codes a Bug Pin
Richard Deeming20-Sep-17 10:36
mveRichard Deeming20-Sep-17 10:36 
You should only need to write this once! Smile | :)
C#
public static class ArrayExtensions
{
    public static void Populate<T>(this T[] array, Func<int, T> createItem)
    {
        if (array == null) throw new ArgumentNullException(nameof(array));
        if (createItem == null) throw new ArgumentNullException(nameof(createItem));
        
        for (int index = 0; index < array.Length; index++)
        {
            array[index] = createItem(index);
        }
    }
    
    public static T[] Create<T>(int length, Func<int, T> createItem)
    {
        if (length < 0) throw new ArgumentOutOfRangeException(nameof(length));
        if (createItem == null) throw new ArgumentNullException(nameof(createItem));
        if (length == 0) return Array.Empty<T>(); // Added in .NET 4.6
        
        T[] result = new T[length];
        Populate(result, createItem);
        return result;
    }
}

Usage:
C#
Client[] clients = ArrayExtensions.Create(10, _ => new Client());

// Or:
Client[] clients = new Client[10];
clients.Populate(_ => new Client());

If you're pre-4.6, you'll need to replace the Array.Empty[^] method:
C#
public static class ArrayExtensions
{
    ...
    
    public static T[] Empty<T>()
    {
        return EmptyArray<T>.Value;
    }
    
    private static class EmptyArray<T>
    {
        public static readonly T[] Value = new T[0];
    }
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer



modified 21-Sep-17 9:33am.

GeneralRe: Marc Codes a Bug Pin
Marc Clifton20-Sep-17 14:48
mvaMarc Clifton20-Sep-17 14:48 
GeneralWhy (ouch) Upper (ouch) Case (ouch) sucks today Pin
Gary Wheeler20-Sep-17 5:41
Gary Wheeler20-Sep-17 5:41 
GeneralRe: Why (ouch) Upper (ouch) Case (ouch) sucks today Pin
Bassam Abdul-Baki20-Sep-17 5:47
professionalBassam Abdul-Baki20-Sep-17 5:47 
GeneralRe: Why (ouch) Upper (ouch) Case (ouch) sucks today Pin
OriginalGriff20-Sep-17 6:02
mveOriginalGriff20-Sep-17 6:02 
GeneralRe: Why (ouch) Upper (ouch) Case (ouch) sucks today Pin
Gary Wheeler20-Sep-17 6:06
Gary Wheeler20-Sep-17 6:06 
GeneralRe: Why (ouch) Upper (ouch) Case (ouch) sucks today Pin
User 1106097920-Sep-17 6:05
User 1106097920-Sep-17 6:05 
GeneralRe: Why (ouch) Upper (ouch) Case (ouch) sucks today Pin
Gary Wheeler20-Sep-17 6:07
Gary Wheeler20-Sep-17 6:07 
GeneralRe: Why (ouch) Upper (ouch) Case (ouch) sucks today Pin
OriginalGriff20-Sep-17 6:23
mveOriginalGriff20-Sep-17 6:23 
GeneralRe: Why (ouch) Upper (ouch) Case (ouch) sucks today Pin
lopatir20-Sep-17 6:09
lopatir20-Sep-17 6:09 
PraiseRe: Why (ouch) Upper (ouch) Case (ouch) sucks today Pin
Gary Wheeler20-Sep-17 6:11
Gary Wheeler20-Sep-17 6:11 
GeneralRe: Why (ouch) Upper (ouch) Case (ouch) sucks today Pin
GenJerDan20-Sep-17 19:59
GenJerDan20-Sep-17 19:59 
GeneralRe: Why (ouch) Upper (ouch) Case (ouch) sucks today Pin
Dan Neely21-Sep-17 2:42
Dan Neely21-Sep-17 2:42 
GeneralRe: Why (ouch) Upper (ouch) Case (ouch) sucks today Pin
Tim Carmichael20-Sep-17 6:19
Tim Carmichael20-Sep-17 6:19 
GeneralRe: Why (ouch) Upper (ouch) Case (ouch) sucks today Pin
Gary Wheeler20-Sep-17 6:21
Gary Wheeler20-Sep-17 6:21 
GeneralRe: Why (ouch) Upper (ouch) Case (ouch) sucks today Pin
Tim Carmichael20-Sep-17 6:37
Tim Carmichael20-Sep-17 6:37 
GeneralRe: Why (ouch) Upper (ouch) Case (ouch) sucks today Pin
Gary Wheeler20-Sep-17 6:44
Gary Wheeler20-Sep-17 6:44 
GeneralRe: Why (ouch) Upper (ouch) Case (ouch) sucks today Pin
TonyManso20-Sep-17 7:31
professionalTonyManso20-Sep-17 7:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.