1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 /*============================================================
9 ** Purpose: implementation of the FormattableStringFactory
12 ===========================================================*/
14 namespace System
.Runtime
.CompilerServices
17 /// A factory type used by compilers to create instances of the type <see cref="FormattableString"/>.
19 public static class FormattableStringFactory
22 /// Create a <see cref="FormattableString"/> from a composite format string and object
23 /// array containing zero or more objects to format.
25 public static FormattableString
Create(string format
, params object?[] arguments
)
29 throw new ArgumentNullException(nameof(format
));
32 if (arguments
== null)
34 throw new ArgumentNullException(nameof(arguments
));
37 return new ConcreteFormattableString(format
, arguments
);
40 private sealed class ConcreteFormattableString
: FormattableString
42 private readonly string _format
;
43 private readonly object?[] _arguments
;
45 internal ConcreteFormattableString(string format
, object?[] arguments
)
48 _arguments
= arguments
;
51 public override string Format
=> _format
;
52 public override object?[] GetArguments() { return _arguments; }
53 public override int ArgumentCount
=> _arguments
.Length
;
54 public override object? GetArgument(int index
) { return _arguments[index]; }
55 public override string ToString(IFormatProvider
? formatProvider
) { return string.Format(formatProvider, _format, _arguments); }