This article is a mirror article of machine translation, please click here to jump to the original article.

View: 4846|Reply: 2

[Source] Introduction to the IParsable interface added to .NET 7

[Copy link]
Posted on 2023-5-2 15:30:15 | | | |
.NET 7 is a new version of .NET that adds an interface called IParsable. This interface helps developers parse strings in their code more easily.

The IParsable interface consists of two methods: Parse and TryParse.

Documentation:https://learn.microsoft.com/zh-cn/dotnet/api/system.iparsable-1

The Parse method is used to resolve a string to a value of a specified type. If resolution fails, an exception is thrown.

The TryParse method is used to resolve a string to a value of a specified type. It does not throw an exception, but returns a boolean value indicating whether the resolution was successful.

For example, the following code demonstrates how to parse a string into an integer using the Parse method:

If we want to use the TryParse method to parse strings, we can use the following code:

In this code, the success variable will contain a boolean value indicating whether the resolution was successful or not. If the resolution is successful, the value variable will contain the parsed integer.

We create a new User class that contains the Name, Age attributes, we wantPass in a comma-separated string to return the User object, as shown in the figure below:



The full code is as follows:


In conclusion, IParsable interfaces can make it easier to parse strings and can make code more concise and readable. If you are using .NET 7 to develop applications, then you should consider using this interface.





Previous:VSTO (III) Inserts DataTable data into a Word document
Next:RabbitMQ: Who will create Queue and Exchange
Posted on 2023-5-2 16:36:44 |
Learn to learn
Posted on 2023-5-4 08:49:19 |
Good to learn
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com