RangeHeaderPair Struct
System.Net.Http.Headers.RangeHeaderPair
Summary
Represents a single range in a Range header.
Namespace
System.Net.Http.Headers
Assembly
System.Net.Http.dll
Syntax
public struct RangeHeaderPair
Constructors
public RangeHeaderPair(long from, long to)
Initializes a new instance of the RangeHeaderPair struct with the specified start and end byte offsets.
public RangeHeaderPair(long from, Nullable<long> to)
Initializes a new instance of the RangeHeaderPair struct with the specified start byte offset and an optional end byte offset.
Properties
public long From { get; }
Gets the start byte offset of the range.
public Nullable<long> To { get; }
Gets the end byte offset of the range. If this value is null, the range extends to the end of the resource.
Methods
public override string ToString()
Returns a string representation of the current object.
public static RangeHeaderPair Parse(string headers)
Parses a string representing a Range header value and returns a RangeHeaderPair instance.
headers: The string to parse.
public static bool TryParse(string headers, out RangeHeaderPair result)
Tries to parse a string representing a Range header value and returns a value that indicates whether the parsing succeeded.
headers: The string to parse.result: When this method returns, contains the parsed RangeHeaderPair instance if the parsing succeeded, or a default RangeHeaderPair if it failed.
true if the string was parsed successfully; otherwise, false.
Remarks
The RangeHeaderPair struct is used to represent a single range specified in the HTTP Range header. This header is used by clients to request a specific portion of a resource.
A RangeHeaderPair can specify a range from a start byte offset to an end byte offset, or it can specify a range starting from a certain byte offset to the end of the resource (indicated by a null value for the To property).
Example of HTTP Range header:
Range: bytes=0-499,1000-1499,2000-
Each comma-separated value in the example above represents a RangeHeaderPair. The last range '2000-' indicates a range from byte 2000 to the end of the resource.
Example
using System;
using System.Net.Http.Headers;
public class Example
{
public static void Main()
{
// Create a range from byte 0 to 499
RangeHeaderPair range1 = new RangeHeaderPair(0, 499);
Console.WriteLine($"Range 1: {range1}"); // Output: Range 1: 0-499
// Create a range from byte 1000 to the end of the resource
RangeHeaderPair range2 = new RangeHeaderPair(1000, null);
Console.WriteLine($"Range 2: {range2}"); // Output: Range 2: 1000-
// Parse a string
string headerString = "500-999";
if (RangeHeaderPair.TryParse(headerString, out RangeHeaderPair parsedRange))
{
Console.WriteLine($"Parsed Range: {parsedRange.From}-{parsedRange.To}"); // Output: Parsed Range: 500-999
}
}
}