

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to initialize a string to an empty string in C#?
To initialize a string to an empty list −
string myStr = null;
Now, use the built-in method IsNullOrEmpty() to check whether the list is empty or not −
if (string.IsNullOrEmpty(myStr)) { Console.WriteLine("String is empty or null!"); }
Let us see the complete code −
Example
using System; namespace Demo { class Program { static void Main(string[] args) { string myStr = null; if (string.IsNullOrEmpty(myStr)) { Console.WriteLine("String is empty or null!"); } Console.ReadKey(); } } }
Output
String is empty or null!
Another way to initialize a string to an empty string, try the following code. Here, we have used string.Empty −
Example
using System; namespace Demo { public class Program { public static void Main(string[] args) { string myStr = string.Empty; if (string.IsNullOrEmpty(myStr)) { Console.WriteLine("String is empty or null!"); } else { Console.WriteLine("String isn't empty or null!"); } } } }
Output
String is empty or null!
- Related Questions & Answers
- How to initialize an empty DateTime in C#
- How to initialize a dictionary to an empty dictionary in C#?
- How to initialize a list to an empty list in C#?
- How to initialize a tuple to an empty tuple in C#?
- How to initialize an empty array list in Kotlin?
- How to declare an empty string array in C#?
- How to remove an empty string from a list of empty strings in C#?
- C# program to create an empty string array
- How to use string.Empty or String.Empty to initialize a string in C#?
- How to create Empty Values String in JavaScript?
- How to update empty string to NULL in MySQL?
- How to check if a string is empty in Kotlin?
- How to test String is null or empty?
- How to remove empty string/lines from PowerShell?
- How to filter empty string values from a Java List?
Advertisements