Some Common Differences in C# Programming

Difference between Concatenation and placeholder Syntax

  1. using System;  
  2. class UserInteractive  
  3. {  
  4.    public static void Main()  
  5.    {  
  6.       string name;  
  7.       Console.WriteLine("What’s Your Name? ");  
  8.       Console.Write ("Enter Your Name: ");  
  9.       name=Console.ReadLine();  
  10.       Console.WriteLine("Hello! "+name+” nice to meet you ”+name”.”); //Concatenation  
  11.       Console.WriteLine(“Hello! {0} nice to meet you {1}.”,name,name); //placeholder  
  12.       // placeholder are most preferable way.  
  13.    }  
  14. }  
Difference between Parse and TryPare 
  • If the no. is in string format you have two option parse and try parse.

  • Parse method throw an exception, if it cannot parse the value.

  • Whereas TryParse() return a bool indication whether it succeeded or failed.

  • Use parse() if you are sure the value use valid other wise use try parse. 
  1. using System;  
  2. //use of parse  
  3. class ParseTesting  
  4. {  
  5.    public static void Main()  
  6.    {  
  7.       string strNumber="100";  
  8.       int i=int.Parse(strNumber);  
  9.       Console.WriteLine(i);  
  10.    }  
  11. }  
  1. using System;  
  2. //use of try parse  
  3. class TryparseTesting  
  4. {  
  5.    public static void Main()  
  6.    {  
  7.       string strNumber="100TG";  
  8.       int Result;  
  9.       bool IsConversionSucessful= int.TryParse(strNumber, out Result);  
  10.       if(IsConversionSucessful)  
  11.       {  
  12.          Console.WriteLine(Result);  
  13.       }  
  14.       else  
  15.       {  
  16.          Console.WriteLine("Please Enter a valid number");  
  17.       }  
  18.    }  
  19. }  
Difference between | and || or & and && 
  • If we use | or & it always check both the condition of statements whether it satisfies it or not it consume too much compiling time.

  • Other hand if we use || or && if left hand condition of statements satisfy them they don’t go to the right hand side it saves the compiling time.

Next Recommended Reading IS And AS Keyword Difference In C#