STRINGS (NULLOREMPTY) IN C#

Today in Engineer's World, we are discussing a very important topic of C# -String in very easy way. Posted By- +Manish Kumar Gautam +LIV WIRE  +ASP.NET SOLUTIONS

Click imaginationhunt.blogspot to see latest Blogs
 
How many ways are there in which we can make our string null or empty?




4 Ways:- Making string Null and Empty

1. Using ""(Double Quotes)
Double Quotes are the way in which we make our string holding zero characters. Its length is zero.
For ex- string str = "";

2. Using "   "(White Spaces)
White spaces are the way in which we make our string storing some data but actually they are blank spaces. They possess some length but in real they contain only blank spaces.

3. Using String.Empty
An empty string means a string that contains zero characters, whose length is zero. It is an instance of System.String object.

Click imaginationhunt.blogspot to see latest Blogs

4. Using NULL
"null" is also used to make empty string. But it is not an instance of System.String object. 
Note:
We cannot directly use null declared variables on method, as it may give NULLREFERENCEEXCEPTION. But what we can do is we can use the null declared variables with some other variables and do various operations.

Let's see each one of them

Practical Implementation:

using System;

namespace AspnetSolutions
{
    class StringNullEmpty
    {
        public static void Main()
        {
            string abc = "";  // using double quotes
            string def = "  ";  //using whitespaces
            string pqr = string.Empty;  // using String.Empty
            string xyz = null;  //using null
              
            // Section-1 (
using double quotes)
            Console.WriteLine("using double quotes");
            Console.WriteLine(abc);
            Console.WriteLine("Length = {0}",abc.Length);
            Console.WriteLine("----------------");

            // Section-2 (using whitespaces)
            Console.WriteLine("using whitespaces");
            Console.WriteLine(def);
            Console.WriteLine("Length = {0}", def.Length);
            Console.WriteLine("----------------");

            // Section-3 (using String.Empty)
            Console.WriteLine("using String.Empty");
            Console.WriteLine(pqr);
            Console.WriteLine("Length = {0}", pqr.Length);
            Console.WriteLine("----------------");

            // Section-4 (using null)
            Console.WriteLine("using null");
            Console.WriteLine(xyz);
            try
            {
                Console.WriteLine("Length = {0}", xyz.Length);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

        }
    }
}

Click imaginationhunt.blogspot to see latest Blogs

Output


As you can see we have shown all the 4 ways separately in 4 sections. By 3 different overloads of WriteLine() method
1. In first overload, we print the section (null, empty,etc) name.
2. In second, we directly print the variable.
3. And in last, we are calculating the size taken by the variable.

One thing that makes the program look weird is- we have use the try-catch block in the last statement when we are calculating the length of the 'null' declared variable 'xyz'. Why?
Because as we said we cannot directly use null declared variables, as it may give NULLREFERENCEEXCEPTION.


Click imaginationhunt.blogspot to see latest Blogs

But then question arises how to use null declared variables? Answer is - we can use with some other variables. 
Let's see this 

Practical Implementation:

using System;

namespace AspnetSolutions
{
    class StringNullEmpty
    {
        public static void Main()
        {
            string def = "  ";  //using whitespaces
            string xyz = null;  //using null    

            string lmn = def + "," + xyz;
            Console.WriteLine(lmn);
            Console.WriteLine("Length = {0}",lmn.Length);

        }
    }
}

Output


By using with some other variable, null declared variable work efficiently with method and properties without throwing error.


Related questions:


Q-1 Which one is the correct method to delete white spaces from string?
A) Trim()
B) Remove()
C) Replace()
D) None of the above
Ans- Option(A).
Explanation- Let's understand by an example
Console.WriteLine("   Gopal  ".Trim());
O/p- Gopal

Q-2 What would be the length if a null declared string is added with a normal string? (Given- Length is 28)

using System;

namespace AspnetSolutions
{
    class StringNullEmpty
    {
        public static void Main()
        {
            string name = "Imaginationhunt.blogspot.com"; //  Given- Length is 28) 
            string lastname = null;

            name=name+lastname; 
            Console.WriteLine(name.Length); 
// Guess the Length ???
        }
    }
}
A) NullReferenceException
B) 28
Ans- Option(B).
Explanation: By using with some other variable (i.e. name), null declared variable (i.e. lastname) work efficiently with method and properties without throwing error.

Click imaginationhunt.blogspot to see latest Blogs

Keep learning and sharing...

Comments

Popular posts from this blog

SSC CGL 2016 Tier-1 Marks / Result

DATA TYPES IN C#

Pattern printing program

IS STRING MUTABLE OR IMMUTABLE IN .NET?

Top Mutual Funds

SSC CGL 2017 TIER 2 RESULT OUT

JUST IN TIME (JIT)

Can we have multiple Main methods in one class

TOP 15 VISUAL STUDIO SHORTCUT KEYS

Top 10 Interview Tips and Questions