zaro

What's the difference between string and string in C#?

Published in C# Strings 3 mins read

What's the Difference Between string and String in C#?

In C#, there is no functional difference between string (lowercase) and String (uppercase); they are fundamentally the same type. The lowercase string is simply an alias in C# for the .NET Framework's System.String class.

Understanding string and String in C

Both string and String refer to the System.String class, which is a fundamental class in the .NET Base Class Library (BCL) used to represent sequences of Unicode characters.

The Alias Relationship

The C# language provides string as a keyword alias for System.String. This is similar to how int is an alias for System.Int32, bool for System.Boolean, or double for System.Double. This aliasing is a convenience feature of the C# language.

Key takeaway: When you declare a variable using string, the compiler treats it exactly the same as if you had used System.String.

Practical Implications and Usage

While functionally identical, there are practical aspects that influence which one developers typically use:

  • Namespace Requirement:
    • Using string (lowercase) does not require a using System; directive at the top of your file because it's a built-in keyword alias recognized directly by the C# compiler.
    • Using String (uppercase) explicitly refers to the System.String class and typically requires a using System; directive or full qualification (System.String).
  • Readability and Consistency: Most C# developers prefer using the string keyword for consistency with other C# primitive type aliases (int, bool, double, etc.). This contributes to cleaner and more uniform code.
  • IntelliSense and IDE Support: Modern IDEs like Visual Studio will recognize both, but string is generally the suggested default for declarations.
  • Functionality: Regardless of which you use for declaration, the underlying System.String class provides the same extensive set of methods for safely creating, manipulating, and comparing strings. These methods include Length, Substring(), Replace(), ToLower(), ToUpper(), Equals(), Compare(), Contains(), and many more.

When to Use Which?

The following table summarizes the key distinctions:

Feature string (C# Keyword) String (.NET Class)
Type C# language alias Full .NET class name (System.String)
Equivalence Functionally identical Functionally identical
Namespace Req. No using System; needed Requires using System; or full path (System.String)
Common Usage More common for declarations in C# code for consistency and convenience. Less common for direct declarations; sometimes seen in older codebases, or when explicitly referencing the BCL type (e.g., via reflection).
Readability Generally preferred for C# idioms. Can be used, but may appear less idiomatic.

In most C# programming scenarios, it is recommended to use the string keyword.

String Manipulation Examples

Here are some common ways to work with strings in C#, demonstrating that both string and String yield the same capabilities:

  1. Declaration:
    string message1 = "Hello, C#!"; // Using the C# keyword alias
    System.String message2 = "Welcome to .NET."; // Using the fully qualified class name
    // Or, with 'using System;' at the top:
    // String message3 = "Learn more.";
  2. Concatenation:
    string combinedMessage = message1 + " " + message2;
    // Output: "Hello, C#! Welcome to .NET."
  3. Using String Methods:
    int length = message1.Length; // Both string and String variables have access to the same methods
    string upperCaseMessage = message2.ToUpper();
    bool startsWithHello = message1.StartsWith("Hello");

In conclusion, while you might encounter both string and String in C# code, understand that they refer to the exact same type and offer identical functionalities for handling text data. The choice typically boils down to coding style and convention, with string being the widely adopted standard within the C# community due to its conciseness and native keyword feel.