Uri uri = new Uri("https://www.example.com/login?username=john&password=doe"); NameValueCollection query = HttpUtility.ParseQueryString(uri.Query); string username = query["username"]; string password = query["password"];
NameValueCollection values = new NameValueCollection(); values.Add("name", "John Smith"); values.Add("age", "30"); values.Add("gender", "male"); string queryString = values.GetQueryString();In this example, we create a NameValueCollection object with some key-value pairs. We then use the GetQueryString method to generate a query string representation of the collection. The resulting string is "name=John+Smith&age=30&gender=male". The System.Collections.Specialized NameValueCollection class is part of the .NET Framework Class Library.