Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

.net 6 how to use constant interpolation string

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces. NET 6 constant interpolation string how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this .NET 6 constant interpolation string article will have something to gain, let's take a look.

The code is as follows:

String scheme = "https"; string host = "xxx.com"; int port = 8080th Console.WriteLine (string.Format ("{0}: / / {1}: {2}", scheme, host, port))

However, this replacement method is prone to errors, such as wrong parameter order, invalid index numbers and so on.

Therefore, the recommended way to write is to use string interpolation, with the following code:

Console.WriteLine ($"{scheme}: / / {host}: {port}")

This makes it easier to read, and the value of the variable is replaced directly into the string.

Constant interpolation string

When all strings are constant, before .NET 6, you cannot use string interpolation, just use + concatenation strings:

In .NET 6, we can already use interpolated strings for constants, as follows:

Const string FirstName = "My"; const string LastName = "IO"; const string FullName = $"{FirstName} {LastName}"

It is important to note that constants in an interpolated string cannot be numbers:

This is because there is a culture distinction when converting numeric constants to strings, which can only be obtained at run time:

Console.WriteLine ($"{1234.56}"); / / output: 1234.56 Thread.CurrentThread.CurrentCulture= new CultureInfo ("es-ES"); Console.WriteLine ($"{1234.56}"); / / output: 1234. Conclusion:

When using parameters for Attribute, constant interpolation of strings is very convenient, as shown in the following code:

Public class xxClass {[Obsolete ($"Use {nameof (NewMethod)} instead")] public void OldMethod () {} public void NewMethod () {}}

In this way, we don't have to hard-code the method name in Message.

This is the end of the article on "how to use .NET 6 constant interpolation strings". Thank you for reading! I believe that everyone has a certain understanding of the ".NET 6 constant interpolation string how to use" knowledge, if you want to learn more knowledge, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report