HttpRangeItemHeaderValue Class

Namespace: System.Net.Http.Headers

Represents a range item in an HTTP Range header.

Syntax

public sealed class HttpRangeItemHeaderValue

Fields

HttpRangeItemHeaderValue does not have publicly accessible fields.

Properties

Name Type Description
From long? Gets the start of the range. If this value is null, the range is an open-ended suffix range (e.g., "bytes=-500").
To long? Gets the end of the range. If this value is null, the range is an open-ended prefix range (e.g., "bytes=500-").

Constructors

Name Description
HttpRangeItemHeaderValue(long? from) Initializes a new instance of the HttpRangeItemHeaderValue class with only a start value, representing an open-ended suffix range.
HttpRangeItemHeaderValue(long? from, long? to) Initializes a new instance of the HttpRangeItemHeaderValue class with a start and end value.

Methods

Name Description
Parse(string input) Parses a string representation of a range item and returns an instance of HttpRangeItemHeaderValue.
ToString() Returns a string representation of the range item.
TryParse(string input, out HttpRangeItemHeaderValue parsedValue) Attempts to parse a string representation of a range item and returns a value that indicates whether the parsing succeeded.

Remarks

This class is used to represent a single item within the HTTP Range header. For example, in the header Range: bytes=0-499, 1000-1499, there are two HttpRangeItemHeaderValue objects: one representing 0-499 and another representing 1000-1499.

The From and To properties allow for specifying both closed ranges (e.g., 0-499) and open-ended ranges (e.g., 0- or -500).

Example

The following example demonstrates how to create and use HttpRangeItemHeaderValue objects.

using System.Net.Http.Headers;

// Create a range from 0 to 499
var range1 = new HttpRangeItemHeaderValue(0, 499);
Console.WriteLine(range1.ToString()); // Output: 0-499

// Create an open-ended suffix range (last 500 bytes)
var range2 = new HttpRangeItemHeaderValue(null, 500);
Console.WriteLine(range2.ToString()); // Output: -500

// Create an open-ended prefix range (from byte 1000 onwards)
var range3 = new HttpRangeItemHeaderValue(1000, null);
Console.WriteLine(range3.ToString()); // Output: 1000-

// Parsing a range string
if (HttpRangeItemHeaderValue.TryParse("200-399", out var parsedRange))
{
    Console.WriteLine($"Parsed From: {parsedRange.From}, To: {parsedRange.To}");
    // Output: Parsed From: 200, To: 399
}

Note: This documentation is a representation of the HttpRangeItemHeaderValue class within the System.Net.Http.Headers namespace, commonly found in .NET development.