Знаете ли вы еще один правильный способ сделать то же самое?Строковый формат с использованием
string initialTemplate = "{0}-{1}";
string template = string.Format(initialTemplate, "first", "{0}");
string answer = string.Format(template, "second");
Также следующим образом фактически известно, но в моем нынешнем случае unfortunatelyI не может использовать этот метод (я думаю, что это путь более правильным и логика более ясно):
string initialTemplate = "{0}-{{0}}";
string template = string.Format(initialTemplate, "first");
string answer = string.Format(template, "second");
Может быть есть еще один намек, как это сделать?
ОБНОВЛЕНИЕ Мне очень жаль, но из ваших ответов Я узнал, что мой вопрос недостаточно ясен. Поэтому я добавил немного больше описания.
Моя ситуация:
//that template is actually placed in *.resx file
//I want storing only one template and use that in different situations
public const string InitialTemplate = "{0}-{1}";
public static string GetMessage(string one, string two)
{
return string.Format(InitialTemplate, one, two);
}
public static string GetTemplate(string one)
{
return string.Format(InitialTemplate, one, "{0}");
}
//or morew universal way
public static string GetTemplate(params object[] args)
{
return string.Format(InitialTemplate, args, "{0}");
}
static void Main(string[] args)
{
//in almost all cases in my project i need to use string.format like this
string message = GetMessage("one", "two");
//but there is another way where i have to use
//the template have already been assigned first argument
//the result must be "one-{0}"
string getTemplateWithAssignedFirstArg = GetTemplate("one");
}
Вы знаете, более правильный путь для такого рода ситуации?
Почему вы не можете использовать этот второй метод? У вас нет доступа для записи к 'initialTemplate'? – dfoverdx