Difference between String and string in .NET Core

πŸ’‘ Concept Name

String vs string in C# / .NET Core

πŸ“˜ Quick Intro

In C#, `string` is an alias (keyword) for `System.String`. Both represent the same .NET type. The choice between them is based on coding conventions or readability preferences.

🧠 Analogy / Short Story

Think of `string` and `String` like "bike" and "Bicycle". Both refer to the same object, just different naming stylesβ€”one's a casual word (alias), the other is formal (class name). Internally, they are identical.

πŸ”§ Technical Explanation

In C#:

  • `string` is a **C# keyword**, an alias for `System.String`.
  • `String` refers to the **.NET class** `System.String` defined in the BCL (Base Class Library).
  • They are functionally identical and interchangeable.
  • Use `string` for variable declarations, and `String` when using static members like `String.IsNullOrEmpty()`.

🎯 Purpose & Use Case

  • βœ… `string` - for declarations and fields
  • βœ… `String` - for accessing static methods like `String.Format()`
  • βœ… Both compile to `System.String`
  • βœ… No performance difference

πŸ’» Real Code Example

// Both lines declare the same type
string name = "Rishu";
String city = "Delhi";

// Both are valid and refer to same .NET type
Console.WriteLine(name.GetType()); // System.String
Console.WriteLine(String.IsNullOrEmpty(city)); // True or False

❓ Interview Q&A

Q1: Is `string` a class?
A: It is a keyword in C# that maps to the class `System.String`.

Q2: Are `string` and `String` interchangeable?
A: Yes, they are completely interchangeable in code.

Q3: Which is preferred for static methods?
A: Use `String` for calling static methods like `String.IsNullOrEmpty()`.

Q4: Which is preferred for declarations?
A: `string` is preferred for readability and consistency in declarations.

Q5: Are there performance differences?
A: No, they compile to the same IL code.

Q6: Can I mix usage of `string` and `String`?
A: Yes, but consistent style is better for readability.

Q7: Is `String` part of System namespace?
A: Yes, it’s `System.String`.

Q8: Is `string` available in VB.NET?
A: No, VB.NET uses `String` only.

Q9: Can I use `String s = null;`?
A: Yes, it’s valid syntax.

Q10: What’s a good convention?
A: Use `string` for type declarations, `String` for static method calls.

πŸ“ MCQs

Q1. What does `string` represent in C#?

  • A C++ class
  • Alias for System.String
  • Static class
  • Value type

Q2. Which namespace does `String` belong to?

  • CSharp
  • DotNet
  • System
  • Core

Q3. Which is recommended for static methods?

  • string
  • String
  • str
  • None

Q4. Is `string` a data type?

  • Yes, it’s a class
  • No, it’s an interface
  • It's an alias
  • It's a struct

Q5. What will `typeof(string)` return?

  • string
  • System.String
  • String
  • string.class

Q6. Are `string` and `String` case-sensitive different types?

  • Yes
  • No
  • Only in .NET Core
  • Only in Framework

Q7. Which one is a .NET class?

  • string
  • String
  • str
  • StringType

Q8. Which is a C# keyword?

  • System.String
  • string
  • System.string
  • String()

Q9. Can you write `String s = null;`?

  • No
  • Yes
  • Only in VB.NET
  • Only with var

Q10. Do they have performance differences?

  • Yes
  • Sometimes
  • No
  • In .NET Core only

πŸ’‘ Bonus Insight

Using `string` for declarations is a widely accepted C# convention, as it improves readability. Reserve `String` for static method access like `String.Concat`, `String.Format`, etc.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

β¬… Previous:

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: