Skip to content

Commit 77d9d04

Browse files
committed
Add NotNullWhen attribute to TryGetCountry overload
Introduced conditional overloads of TryGetCountry in ICountryProvider and CountryProvider for .NET Standard 2.1+ and .NET Core 3.0+, using [NotNullWhen(true)] for improved nullability annotations. Updated XML documentation for interface methods to clarify parameters, return values, and exceptions.
1 parent 53b5931 commit 77d9d04

File tree

2 files changed

+82
-17
lines changed

2 files changed

+82
-17
lines changed

src/Nager.Country/CountryProvider.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
using System;
33
using System.Collections.Generic;
44

5+
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_0_OR_GREATER
6+
using System.Diagnostics.CodeAnalysis;
7+
#endif
8+
59
namespace Nager.Country
610
{
711
/// <summary>
@@ -304,6 +308,8 @@ public ICountryInfo GetCountry(string alpha2or3Code)
304308
throw new UnknownCountryException($"Cannot found a country for code {alpha2or3Code}");
305309
}
306310

311+
#if NETSTANDARD2_0 || NET48
312+
307313
/// <inheritdoc/>
308314
public bool TryGetCountry(string alpha2or3Code, out ICountryInfo? countryInfo)
309315
{
@@ -323,6 +329,31 @@ public bool TryGetCountry(string alpha2or3Code, out ICountryInfo? countryInfo)
323329
return false;
324330
}
325331

332+
#endif
333+
334+
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_0_OR_GREATER
335+
336+
/// <inheritdoc/>
337+
public bool TryGetCountry(string alpha2or3Code, [NotNullWhen(true)] out ICountryInfo? countryInfo)
338+
{
339+
if (Enum.TryParse(alpha2or3Code, true, out Alpha2Code alpha2Code))
340+
{
341+
countryInfo = this.GetCountry(alpha2Code);
342+
return true;
343+
}
344+
345+
if (Enum.TryParse(alpha2or3Code, true, out Alpha3Code alpha3Code))
346+
{
347+
countryInfo = this.GetCountry(alpha3Code);
348+
return true;
349+
}
350+
351+
countryInfo = null;
352+
return false;
353+
}
354+
355+
#endif
356+
326357
/// <inheritdoc/>
327358
public ICountryInfo GetCountry(Alpha2Code alpha2Code)
328359
{
Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,78 @@
11
using System.Collections.Generic;
22

3+
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_0_OR_GREATER
4+
using System.Diagnostics.CodeAnalysis;
5+
#endif
6+
37
namespace Nager.Country
48
{
59
/// <summary>
6-
/// ICountryProvider
10+
/// CountryProvider Interface
711
/// </summary>
812
public interface ICountryProvider
913
{
1014
/// <summary>
11-
/// Get all country informations
15+
/// Retrieves information for all available countries.
1216
/// </summary>
13-
/// <returns></returns>
17+
/// <returns>An <see cref="IEnumerable{ICountryInfo}"/> containing all country information.</returns>
1418
IEnumerable<ICountryInfo> GetCountries();
1519

1620
/// <summary>
17-
/// Get country by alpha2 or alpha3 code
21+
/// Retrieves information for a specific country by its ISO alpha-2 or alpha-3 code.
1822
/// </summary>
19-
/// <param name="alpha2or3Code"></param>
20-
/// <returns></returns>
23+
/// <param name="alpha2or3Code">The ISO alpha-2 or alpha-3 code of the country.</param>
24+
/// <returns>The <see cref="ICountryInfo"/> corresponding to the specified code.</returns>
25+
/// <exception cref="UnknownCountryException">Thrown if no country matches the provided code.</exception>
2126
ICountryInfo GetCountry(string alpha2or3Code);
2227

28+
#if NETSTANDARD2_0 || NET48
29+
2330
/// <summary>
24-
/// Try get country by alpha2 or alpha3 code
31+
/// Attempts to retrieve country information by its ISO alpha-2 or alpha-3 code.
2532
/// </summary>
26-
/// <param name="alpha2or3Code"></param>
27-
/// <param name="countryInfo"></param>
28-
/// <returns></returns>
33+
/// <param name="alpha2or3Code">The ISO alpha-2 or alpha-3 code of the country.</param>
34+
/// <param name="countryInfo">
35+
/// When this method returns, contains the <see cref="ICountryInfo"/>
36+
/// for the specified code if found; otherwise, null.
37+
/// </param>
38+
/// <returns>
39+
/// <c>true</c> if a country matching the specified code was found; otherwise, <c>false</c>.
40+
/// </returns>
2941
bool TryGetCountry(string alpha2or3Code, out ICountryInfo? countryInfo);
3042

43+
#endif
44+
45+
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_0_OR_GREATER
46+
47+
/// <summary>
48+
/// Attempts to retrieve country information by its ISO alpha-2 or alpha-3 code.
49+
/// </summary>
50+
/// <param name="alpha2or3Code">The ISO alpha-2 or alpha-3 code of the country.</param>
51+
/// <param name="countryInfo">
52+
/// When this method returns, contains the <see cref="ICountryInfo"/>
53+
/// for the specified code if found; otherwise, null.
54+
/// </param>
55+
/// <returns>
56+
/// <c>true</c> if a country matching the specified code was found; otherwise, <c>false</c>.
57+
/// </returns>
58+
bool TryGetCountry(string alpha2or3Code, [NotNullWhen(true)] out ICountryInfo? countryInfo);
59+
60+
#endif
61+
3162
/// <summary>
32-
/// Get country by alpha2 code
63+
/// Retrieves information about a country using its ISO 3166-1 alpha-2 code.
3364
/// </summary>
34-
/// <param name="alpha2Code"></param>
35-
/// <returns></returns>
65+
/// <param name="alpha2Code">The ISO alpha-2 code of the country.</param>
66+
/// <returns>The <see cref="ICountryInfo"/> corresponding to the specified code.</returns>
67+
/// <exception cref="UnknownCountryException">Thrown if no country matches the provided code.</exception>
3668
ICountryInfo GetCountry(Alpha2Code alpha2Code);
3769

3870
/// <summary>
39-
/// Get country by alpha3 code
71+
/// Retrieves information about a country using its ISO 3166-1 alpha-3 code.
4072
/// </summary>
41-
/// <param name="alpha3Code"></param>
42-
/// <returns></returns>
73+
/// <param name="alpha3Code">The ISO alpha-3 code of the country.</param>
74+
/// <returns>The <see cref="ICountryInfo"/> corresponding to the specified code.</returns>
75+
/// <exception cref="UnknownCountryException">Thrown if no country matches the provided code.</exception>
4376
ICountryInfo GetCountry(Alpha3Code alpha3Code);
4477

4578
/// <summary>
@@ -50,7 +83,8 @@ public interface ICountryProvider
5083
/// GetCountryByNameConsiderTranslation -> <see href="https://github.com/nager/Nager.Country/blob/master/src/Nager.Country.Translation/CountryProviderExtension.cs>">CountryProviderExtension</see>
5184
/// </remarks>
5285
/// <param name="countryName"></param>
53-
/// <returns></returns>
86+
/// <returns>The <see cref="ICountryInfo"/> corresponding to the specified code.</returns>
87+
/// <exception cref="UnknownCountryException">Thrown if no country matches the provided code.</exception>
5488
ICountryInfo GetCountryByName(string countryName);
5589
}
5690
}

0 commit comments

Comments
 (0)