Saturday, January 17, 2015

Delete Duplicate Rows from DataTable in C#

 You have a Data Table-dt
 To remove duplicate records from dt.

for (int i = 0; i < dt.Rows.Count-1; i++) 
{ 
  for (int j = i+1; j < dt.Rows.Count; j++) 
  { 
    if (dt.Rows[i].ItemArray.SequenceEqual(dt.Rows[j].ItemArray)) 
      dt.Rows[j].Delete(); 
  } 
} 
»Keep Sharing and Learning«

Tuesday, January 6, 2015

Difference between String and string

Let's Discuss difference between

                    String  and  string in c#
in one word :-

                          String is a class in dotnet class library
                          
                          string is a keyword

illustration ::

                    Here we go,consider the following program


//program1 
//program for just declaring a string variable 
using System; 
class Program 
{ 
static void Main(string[] args) 
  { 
  String str = "string vs String"; 
  } 
}



above program will run without any error but doesn't have any output.

Now  i remove the namespace System from the program,the code will looks like


//program2 
//program for just declaring a string variable 
class Program 
{ 
static void Main(string[] args) 
  { 
  String str = "string vs String"; 
  } 
}


if i try to run the program it will shows following error message
from the above two program we understood that

  • String is class inside System Namespace
  • Since You must import System to use String
Now if i replace String with string in program2 as below


//program3 
//program for just declaring a string variable 
class Program 
{ 
static void Main(string[] args) 
{ 
string str = "string vs String"; 
} 
}



program will run perfectly,So we may Conclude that
string keyword = System.String class

As always keywords always make the things more Simple.That means you can use variables of type string without using System namespace.


Done
»Keep Sharing and Learning«

video tutorial

Saturday, January 3, 2015

How to measure program execution time in C#

Please find original post from here

Tip showing how to measure time taken by a C# program using the class Stopwatch.

Stopwatch class provides a set of methods and properties that you can use to accurately measure elapsed time-by msdn

using System;
//don't forget to use namespace System.Diagnostics
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //creating object of Stopwatch
            Stopwatch myTimer = new Stopwatch();
            //strats timer
            myTimer.Start();
            //your program goes here
            //loop to add numbers between 0 and 10000
            int x = 0;
            for (int i = 0; i < 10000; i++)
            {
                x += i;
            }
            //stop timer after specific code under evaluation
            myTimer.Stop();
            //print time elapsed by the code in between Start() and Stop() methods.
            Console.WriteLine("time elapsed :" + myTimer.Elapsed);
            Console.ReadKey();
        }
    }
}
output for above code snippet was:-

Please find original post from here

-- Done --
»Keep Sharing and Learning«

video tutorial