0

I have such piece of code:

    string suffix = "wallpapers\\";
    string extenstion = ".jpg";
    string[] wallpapers;

    .....
    void SetWallPapers()
    {
        wallpapers = new string[] {
            suffix + "1" + extenstion,
            suffix + "2" + extenstion,
            suffix + "3" + extenstion,
            suffix + "4" + extenstion,
        };
    }

Is there any variant to make lambda-declaretion in array content like:

( pseudo-code, idea only! )

wallpapers = new string[] { ( () => { for i = 1 till 4 -> suffix + i + extension; } ) }

Any suggestions?

5
  • 1
    A 'suffix' is something you add to the end of something, you might want to change it to 'prefix' (or maybe 'path' in this case). Commented Sep 5, 2012 at 9:58
  • @Joe , no I don't, but now have, thanks to all :) Commented Sep 5, 2012 at 10:35
  • 1
    I'm constantly amazed by how many people can give an [almost] identical answer. Commented Sep 5, 2012 at 11:10
  • @Joe they all are different in a ways how to format string of image-path Commented Sep 5, 2012 at 11:42
  • 2
    Yes. All different techniques for joining a string together with an identical outcome. Some are better practise than others but still, lots of duplicates. This happens a lot on StackOverflow, I never know why. I always upvote the first correct one. Commented Sep 5, 2012 at 11:43

5 Answers 5

1
string[] wallpapers = Enumerable.Range(1, 4)
                                .Select(i => suffix + i + extenstion)
                                .ToArray();
Sign up to request clarification or add additional context in comments.

Comments

0
string template = "wallpapers\\{0}.jpg";

string[] wallpapers =
    Enumerable.Range(1, 4)
        .Select(i => string.Format(template, i))
        .ToArray();

Comments

0

You can do it using Linq like so:

wallpapers =
    Enumerable.Range(1, 4)
    .Select(i => String.Concat(suffix, i, extenstion))
    .ToArray();

Comments

0

No, there is not. If you're ok with generating your array at runtime, use

Enumerable.Range(1, 4).Select(i => string.Format("{0}{1}{2}", suffix, i, extension)).ToArray();

PS Your code can't work anyway, you can't add int to string.

Comments

0

Enumerable.Range(1, 10).Select(a => "string" + a).ToArray();

Comments

Your Answer

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