HttpLanguageWithQuality Class
Assembly: System.Net.Http.Headers
Inheritance: object → HttpLanguageWithQuality
Represents a language tag with an associated quality value used in HTTP Accept-Language headers.
Syntax
public sealed class HttpLanguageWithQuality
Remarks
The HttpLanguageWithQuality class is used to represent a language tag (e.g., "en-US", "fr") along with a quality value (q) as defined in RFC 7231. This quality value indicates the user's preference for that language.
The quality value is a floating-point number between 0 and 1, where 1 indicates the highest preference and 0 indicates no preference.
Constructors
HttpLanguageWithQuality(string language)
Initializes a new instance of the HttpLanguageWithQuality class with the specified language tag and a default quality value of 1.
public HttpLanguageWithQuality(string language)
Parameters
language: A string that represents the language tag.
HttpLanguageWithQuality(string language, double quality)
Initializes a new instance of the HttpLanguageWithQuality class with the specified language tag and quality value.
public HttpLanguageWithQuality(string language, double quality)
Parameters
language: A string that represents the language tag.quality: A double that represents the quality value, which must be between 0.0 and 1.0 inclusive.
Properties
Language
Gets the language tag.
public string Language { get; }
Quality
Gets the quality value associated with the language tag.
public double Quality { get; }
Methods
ToString()
Returns a string representation of the HttpLanguageWithQuality object.
public override string ToString()
Returns: A string that represents the language tag and its quality value (e.g., "en-US;q=0.8").
Parse(string input)
Parses a string representation of an HttpLanguageWithQuality object.
public static HttpLanguageWithQuality Parse(string input)
Parameters
input: A string representing the language tag and quality value.
Returns: An HttpLanguageWithQuality object parsed from the input string.
Throws: FormatException if the input string is not a valid representation of an HttpLanguageWithQuality.
Example
C# Example
using System;
using System.Net.Http.Headers;
public class Example
{
public static void Main(string[] args)
{
// Create a language tag with default quality (1.0)
HttpLanguageWithQuality english = new HttpLanguageWithQuality("en-US");
Console.WriteLine($"Language: {english.Language}, Quality: {english.Quality}");
Console.WriteLine($"String representation: {english.ToString()}");
// Create a language tag with a specific quality
HttpLanguageWithQuality french = new HttpLanguageWithQuality("fr", 0.7);
Console.WriteLine($"Language: {french.Language}, Quality: {french.Quality}");
Console.WriteLine($"String representation: {french.ToString()}");
// Parse a string into an HttpLanguageWithQuality object
string acceptLanguageString = "es;q=0.9";
try
{
HttpLanguageWithQuality spanish = HttpLanguageWithQuality.Parse(acceptLanguageString);
Console.WriteLine($"Parsed Language: {spanish.Language}, Parsed Quality: {spanish.Quality}");
}
catch (FormatException ex)
{
Console.WriteLine($"Error parsing '{acceptLanguageString}': {ex.Message}");
}
}
}