viahero
viahero

Reputation: 3

How to Use FontFamily in a concise manner

I'm new to C# programming and I have a function involving FontFamily. The code seems to work fine but I wanted it written in a more concise manner.

I searched online but seems unable to get the right solution. The following code is what I currently have.

public FontFamily[] FontFamilyExt()
    {

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

        return f;
    }

I am looking for something that works similarly but uses less codes. Thank you in advance.

Upvotes: 0

Views: 43

Answers (2)

Jonathan
Jonathan

Reputation: 5028

You could use a loop as recommended. You could also use Enumerable.Range, which basically creates a loop under the covers, but will put the code into one line. Something like:

using System.Linq;

public FontFamily[] FontFamilyExt()
{
  return Enumerable.Range(0, 32).Select(x => new FontFamily(_fnt[x])).ToArray();
}

Upvotes: 1

user12031933
user12031933

Reputation:

You can try this:

public FontFamily[] FontFamilyExt()
{
  FontFamily[] f = new FontFamily[32];
  for ( int index = 0; index <= 31; index++ )
    f[index] = new FontFamily(_fnt[index]);
  return f;
}

Upvotes: 0

Related Questions