HttpLanguageRanges Class

Namespace: System.Net.Http.Headers
Assembly: System.Net.Http.Primitives.dll

Represents the value of an HTTP Accept-Language header. This class defines a collection of language ranges with optional quality values, allowing clients to specify their language preferences and the server to select the most appropriate language for the response.

Syntax

public sealed class HttpLanguageRanges : IEnumerable<HttpLanguageWithQuality>, ICollection<HttpLanguageWithQuality>, IList<HttpLanguageWithQuality>

Remarks

The Accept-Language header is used by the client to indicate the set of natural languages that are preferred as a response. The header value consists of a comma-separated list of language tags, each optionally followed by a weight (quality value).

For example: en-US,en;q=0.8,fr;q=0.6.

This class provides a convenient way to parse, manipulate, and serialize language range values. It implements IEnumerable<HttpLanguageWithQuality>, making it easy to iterate over the individual language ranges.

Constructors

HttpLanguageRanges()

Initializes a new instance of the HttpLanguageRanges class with an empty collection.

public HttpLanguageRanges();

Properties

Count

Gets the number of elements contained in the HttpLanguageRanges collection.

public int Count { get; }

IsReadOnly

Gets a value indicating whether the HttpLanguageRanges collection is read-only.

public bool IsReadOnly { get; }

Methods

Add(HttpLanguageWithQuality item)

Adds an item to the end of the HttpLanguageRanges collection.

public void Add(HttpLanguageWithQuality item);

Clear()

Removes all items from the HttpLanguageRanges collection.

public void Clear();

Contains(HttpLanguageWithQuality item)

Determines whether an element is in the HttpLanguageRanges collection.

public bool Contains(HttpLanguageWithQuality item);

CopyTo(HttpLanguageWithQuality[] array, int arrayIndex)

Copies the elements of the HttpLanguageRanges collection to an array, starting at the specified array index.

public void CopyTo(HttpLanguageWithQuality[] array, int arrayIndex);

GetEnumerator()

Returns an enumerator that iterates through the HttpLanguageRanges collection.

public IEnumerator<HttpLanguageWithQuality> GetEnumerator();

IndexOf(HttpLanguageWithQuality item)

Determines the zero-based index of the specified item in the HttpLanguageRanges collection.

public int IndexOf(HttpLanguageWithQuality item);

Insert(int index, HttpLanguageWithQuality item)

Inserts an item into the HttpLanguageRanges collection at the specified index.

public void Insert(int index, HttpLanguageWithQuality item);

Parse(string input)

Parses a string representation of an Accept-Language header and returns an HttpLanguageRanges object.

public static HttpLanguageRanges Parse(string input);

Example:


var headerValue = "en-US,en;q=0.9,fr;q=0.7,es;q=0.5";
HttpLanguageRanges languageRanges = HttpLanguageRanges.Parse(headerValue);

Console.WriteLine($"Parsed {languageRanges.Count} language ranges:");
foreach (var range in languageRanges)
{
    Console.WriteLine($"- Language: {range.Language}, Quality: {range.Quality}");
}
                    

Remove(HttpLanguageWithQuality item)

Removes the first occurrence of a specified object from the HttpLanguageRanges collection.

public bool Remove(HttpLanguageWithQuality item);

RemoveAt(int index)

Removes the item at the specified index from the HttpLanguageRanges collection.

public void RemoveAt(int index);

ToString()

Converts the value of the current instance to its equivalent string representation.

public override string ToString();

TryParse(string input, out HttpLanguageRanges? result)

Tries to parse a string representation of an Accept-Language header and returns a value that indicates whether the parsing succeeded.

public static bool TryParse(string input, out HttpLanguageRanges? result);

Implements

System.Collections.Generic.IEnumerable<System.Net.Http.Headers.HttpLanguageWithQuality>

System.Collections.Generic.ICollection<System.Net.Http.Headers.HttpLanguageWithQuality>

System.Collections.Generic.IList<System.Net.Http.Headers.HttpLanguageWithQuality>

See Also