YOUR AD GOES HERE

084 - How to C# String Null

Published 12, Aug 2017

The Engineering Projects


Description:
How to handle null String in C#?

A C# string is an array of characters declared using the string keyword. String objects are immutable, meaning that they cannot be changed once they have been created.

What is C# null ?

The null keyword is a special case for a variable value. The implementation of C# on the CLR represents a null reference by zero bits. When defining a string in a class, dont initialize it to null. Instead, initialize it to the constant string.Empty

What is Empty Strings ?

An empty string is an instance of a System.String object that contains zero characters. You can call methods on empty strings because they are valid System.String objects.

string s = "";
What is Null Strings ?

A null string does not refer to an instance of a System.String object and any attempt to call a method on a null string results in a NullReferenceException. e.g. given below

string str = null;
int len = str.Length;
When run the above code it will throw NullReferenceException.

How to check null String in c#?

You may use the null keyword to check or assign the value of an object.

string str =null ;
if (str == null)
{
MessageBox.Show("String is null");
}
In the above code we created a string Object and assigned null and next we check the string is null or not.

IsNullOrEmpty method

IsNullOrEmpty is a convenience method that enables you to simultaneously test whether a String is null or its value is Empty.

string str =null;
if (string.IsNullOrEmpty(str))
{
MessageBox.Show("String is empty or null");
}

Moreover, you should also visit our:

Website: http://www.TheEngineeringProjects.com/
Blog: http://www.theengineeringprojects.com/blog
Shop: http://www.theengineeringprojects.com/shop
FbPage: https://www.facebook.com/theengineeringprojects/
Twitter: https://twitter.com/SyedZainNasir
Google+ : https://plus.google.com/+SyedZainNasir
YouTube: http://youtube.com/TheEngineeringProjects
Email: help@theengineeringprojects.com
Skype: theenggprojects
Mobile / WhatsApp : +92-332-6062060

Thanks for Watching.

Releted More Videos

You May Also Like

YOUR AD GOES HERE

YOUR AD GOES HERE