Uri.CheckSchemeName(String) Method in C#

The Uri.CheckSchemeName() method in C# is used to determine whether the specified scheme name is valid. A valid scheme name must start with a letter and can contain only letters, digits, plus (+), period (.), or hyphen (-) characters.

Syntax

Following is the syntax −

public static bool CheckSchemeName(string schemeName);

Parameters

  • schemeName − The scheme name to validate as a string.

Return Value

Returns true if the scheme name is valid; otherwise, false.

Valid vs Invalid Scheme Names Valid Schemes http, https ftp, file custom-scheme Invalid Schemes 123abc (starts with digit) ht@tp (invalid character) null or empty

Using CheckSchemeName with URI Schemes

Example

using System;
public class Demo {
   public static void Main(){
      Uri newURI = new Uri("https://www.tutorialspoint.com");
      Console.WriteLine("URI = "+newURI);
      string schemeName = newURI.Scheme;
      if(Uri.CheckSchemeName(schemeName))
         Console.WriteLine("Valid: Scheme name");
      else
         Console.WriteLine("Invalid: Scheme name");
   }
}

The output of the above code is −

URI = https://www.tutorialspoint.com/
Valid: Scheme name

Using CheckSchemeName with Custom Schemes

Example

using System;
public class Demo {
   public static void Main(){
      string[] schemes = {"http", "ftp", "custom-scheme", "123invalid", "ht@tp", ""};
      
      foreach(string scheme in schemes) {
         bool isValid = Uri.CheckSchemeName(scheme);
         Console.WriteLine($"Scheme '{scheme}': {(isValid ? "Valid" : "Invalid")}");
      }
   }
}

The output of the above code is −

Scheme 'http': Valid
Scheme 'ftp': Valid
Scheme 'custom-scheme': Valid
Scheme '123invalid': Invalid
Scheme 'ht@tp': Invalid
Scheme '': Invalid

Validating Scheme Names Before URI Creation

Example

using System;
public class Demo {
   public static void Main(){
      string customScheme = "myapp";
      string host = "example.com";
      
      if(Uri.CheckSchemeName(customScheme)) {
         string uriString = $"{customScheme}://{host}";
         Console.WriteLine($"Creating URI with valid scheme: {uriString}");
         try {
            Uri customUri = new Uri(uriString);
            Console.WriteLine($"URI created successfully: {customUri}");
         }
         catch(Exception ex) {
            Console.WriteLine($"Error creating URI: {ex.Message}");
         }
      }
      else {
         Console.WriteLine($"Invalid scheme name: {customScheme}");
      }
   }
}

The output of the above code is −

Creating URI with valid scheme: myapp://example.com
URI created successfully: myapp://example.com/

Conclusion

The Uri.CheckSchemeName() method validates scheme names according to RFC standards. It ensures scheme names start with a letter and contain only valid characters (letters, digits, +, ., -). This method is useful for validating custom schemes before creating URIs.

Updated on: 2026-03-17T07:04:35+05:30

203 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements