相关疑难解决方法(0)

如何创建更加用户友好的string.format语法?

我需要在程序中创建一个非常长的字符串,并且一直在使用String.Format.我遇到的问题是当你有超过8-10个参数时跟踪所有数字.

是否有可能创建某种形式的重载,接受类似于此的语法?

String.Format("You are {age} years old and your last name is {name} ",
{age = "18", name = "Foo"});
Run Code Online (Sandbox Code Playgroud)

c# string

45
推荐指数
1
解决办法
3758
查看次数

对于具有许多参数的长字符串,String.Format最具可读性的用途是什么?

例如:

String login = String.Format("computer={0}&ver={1}.{2}.{3}&from={4}&realcomputername={5}&type={6}&Channels={7}&Hotkeys={8}&ID={9}\r\n",
            serviceConfig.Computer,
            serviceConfig.Version.Major,
            serviceConfig.Version.Minor,
            serviceConfig.Version.Build,
            userName,
            Environment.MachineName,
            type,
            serviceConfig.ChannelsString,
            serviceConfig.HotKeysString,
            serviceConfig.AlarmGroupName);
Run Code Online (Sandbox Code Playgroud)

这不会产生非常易读的代码,并且随着越来越多的参数被添加,它看起来更加丑陋,并且更难以找到哪个参数在哪个槽中.

我知道这是一个noob问题,我想我只是要求如何格式化文本以使其更具可读性,但如果有更好的方法可以做到这一点,我也想知道.

.net c# string.format readability

1
推荐指数
1
解决办法
202
查看次数

命名参数 Console.WriteLine c#

我经常写一个 Console.WriteLine 语句,然后在稍后阶段修改它。然后我最终得到这样的陈述。

Console.WriteLine("{2} is not greater than {0} or smaller than {1}",min,max, grade);
Run Code Online (Sandbox Code Playgroud)

有没有办法显式命名传递给 Console.WriteLine 的参数,这样我就不需要按顺序传递 min、max 和 Grade?

作为相关说明,我经常删除要在 Console.WriteLine 中打印的变量,如下所示,其中 {2} 已被删除。

Console.WriteLine("{3} is not greater than {0} or smaller than {1}",min,max, grade);
Run Code Online (Sandbox Code Playgroud)

有没有办法不改变编号并说如下:

Console.WriteLine("{3} is not greater than {0} or smaller than {1}",{0}:min,{1}:max, {3}: grade);
Run Code Online (Sandbox Code Playgroud)

类似于这样的语句

@Html.ActionLink("Your profile", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" });
Run Code Online (Sandbox Code Playgroud)

c# console.writeline

1
推荐指数
1
解决办法
1470
查看次数