Is there a way to reduce the verbosity of using String.Format (...., p1, p2, p3)?

I often use String.Format()it because it makes line building more readable and manageable.

Is there a way to reduce its syntactic verbosity, for example. with extension, etc.?

Logger.LogEntry(String.Format("text '{0}' registered", pair.IdCode));

public static void LogEntry(string message)
{
    ...
}

eg. I would like to use all my and other methods that get the string the way I use Console.Write(), for example:

Logger.LogEntry("text '{0}' registered", pair.IdCode);
+3
source share
5 answers

What about:

static void LogEntry(string format, params object[] args) {
    Console.WriteLine(format, args); // For example.
}

Now you can call it like this:

Logger.LogEntry("text '{0}' registered", pair.IdCode);
+13
source

Logger.LogEntry, , string.format. paramarray, !

+4

, FormatWith, , :

Logger.LogEntry("I hate my {0}".FormatWith(itemName));

, : http://james.newtonking.com/archive/2008/03/27/formatwith-string-format-extension-method.aspx

+2

Logger.LogEntry , no; . , :

public static void LogEntry(string format, params object[] args) {
    ... string.Format(format,args) ...
}
+1

params, String.Format.

static void FormatString(string myString, params string[] format)
{
     Console.WriteLine(String.Format(myString, format));
}
0

Source: https://habr.com/ru/post/1744178/


All Articles