gpt4 book ai didi

C# string.Format 抛出异常

转载 作者:行者123 更新时间:2023-12-05 08:20:52 25 4
gpt4 key购买 nike

我正在尝试使用 string.Format 格式化一个 string 但它抛出了一个异常。

 var format = "public {0} {1} { get; {2}set; }";
var arg0 = "long";
var arg1 = "Ticks";

var formatedString = string.Format(format, arg0, arg1, null);

最后一行抛出一个 System.FormatException 并包含以下详细信息:

    System.FormatException was unhandled
HResult=-2146233033
Message=Input string was not in a correct format.
Source=mscorlib
StackTrace:
at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
at System.String.Format(String format, Object arg0, Object arg1, Object arg2)
at ConsoleApplication1.Program.Main(String[] args) in E:\lab\cheque\helloworldprism\ConsoleApplication1\ConsoleApplication1\Program.cs:line 11
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

最佳答案

它不喜欢{ get中的{}; {2}设置; 你必须使用其中两个来转义花括号

var format = "public {0} {1} {{ get; {2}set; }}";

关于C# string.Format 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34987813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com