Click here to Skip to main content
15,867,851 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i'm not sure how to describe my self (not sure what is the name of this issue)

i can write to console like this

Console.WriteLine("The answer of {0} + {1} is {2}",2,2,2+2);

how exactly this topic (of {0}... and the arguments) is called ?



any way, i was wondering if i can write something like this.



Dictionary <string,int> dic = new ...

string [] arr = {"hello","world"};

foreach (var str in arr)
{
dic.Add({0},str, str.Length);
}

I know that it's not the correct syntax, but i hope i was understandable enough... :)
Posted
Comments
AndreFratelli 2-Jul-11 12:07pm    
Actually, no, I don't quite get your questions...

In the first one are you trying to know how the {n} marks associate with the arguments? If so, take a look at varargs, you could write something similar with it.

As to the second one, empirically I would say you are trying to insert something at the beginning of a dictionary. Is that it? If so, then it makes no sense because dictionaries are not ordered.
Sergey Alexandrovich Kryukov 2-Jul-11 20:24pm    
Not clear. Your syntax in sample is almost correct, but it does not explain how this is related to format. Perhaps, post some example if format string and content of the dictionary. Are you sure you know what exactly do you want?
--Sa

We could format item from string array and add back to the Dictionary, please see below code,

C#
public static IDictionary<string, int> StringFormmater = new Dictionary<string, int>()
{
};

static void Main(string[] args)
{
    string[] stringArray = { "C# {0} and {1}", "FrameWork {0} and {1}" };
    Array.ForEach(stringArray, item =>
    {
        StringFormmater.Add(string.Format(item, "Injected","From For loop"), item.Length);
    });

}


:)
 
Share this answer
 
dic.Add(string.Format("{0}", str), str.Length);
 
Share this answer
 
Comments
igalep132 2-Jul-11 12:09pm    
thanks... :)
Sergey Alexandrovich Kryukov 2-Jul-11 20:22pm    
Formally correct, but very confusing and totally redundant: "string.Format("{0}", str)" is exactly the same as just "str".

So, you had to write dic.Add(str, str.Lengh); That's it. Sorry, I vote 1 for... how to call it?.. you understand.
--SA
igalep132 3-Jul-11 1:59am    
in case and my kkey is only one string, then you right, just "str" would be enough.
but otherwise i'll need to use string builder...
Sergey Alexandrovich Kryukov 3-Jul-11 2:47am    
I only say, the whole format call is equivalent to 'str'. If cannot be enough or not enough, I just explain the code is absurd.
Are you going to explain what you want to achieve?!
--SA
Matt U. 3-Jul-11 10:38am    
I honestly don't know why I did that, haha. I apologize for the slight misdirection.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900