Posts

Showing posts from 2016

Reading and writing a CSV file in c # using csvHelper Example

Below is an example to write and read a csv file using csvHelper Library in c sharp. First you will have to include the csvHelper lib in to your project by going in to NuGet packages in your project and adding it. Once you don’t that you can examine the below program and implement the program. NB:- The below is just a snippet of code and the objects are not declared properly. So please make necessary changes before running the program. For writing a file- public void WriteFile( Data data)         {             String pathIp = “input_your_filePath”;             StreamWriter writeIp = new StreamWriter (pathIp);             CsvWriter cswIp = new CsvWriter (writeIp);                                                 // this below line is for inserting a header in your CSV file. I am just writing “Data-table”             cswIp.WriteField( "Data-table" );             cswIp.NextRecord();             for ( int i = 0; i < data_length_rows;

Performance evaluation of reading/writing data from CSV and Excel in C sharp

Recently I did a performance evaluation(not part of my plan) on the performance In reading CSV and Excel files and there was a clear winner. I did this test because I had to process a hell lot of data which was given to me was in excel format. I used C# as my platform to start and immediately I found some libraries such as Interop to help me out. It was pretty easy. At the beginning I used only a small portion of the data and when I started using the original files which has like thousands of records, it started to take a lot of time. So I has a preprocessing function where I write this data to a CSV file. And when I started to read from the CSV, it was very fast. Instead of taking some five minutes, now it take less than 10 seconds. I used the csvHelper lib to help me out and it was pretty easy. Even though it might be a one time execution in the production system, while you are doing the coding you might have to execute it a hundred times. So I would suggest to convert it into a CS

How to return two objects at the same time in c sharp or Example of Tuple in c sharp

I found ‘Tuple’ to be  a very special , where you want to return two objects in the same function itself. I will explain with the help of below code. My aim in the below code is to return two objects in the same function. Lets consider those two objects are emp_Permanent and emp_Temp which I need when I call a function. These two objects are of the class Employee. public Tuple < Employee , Employee > splitEmployee( Employee employee)         {                                                                               // here I am splitting my employee objects in to two other objects                                                        // let’s assume that the new objects are emp_Permanent and emp_Temp                                                                       return new Tuple < Employee , Employee >( emp_Permanent, emp_Temp );         } Now in your main code, call this function as below. var splitData =  splitEmp

How to calculate the run time/time required for a program in visual studio or in C Sharp

I recently started programming in C# and I found it far better than Java already. To find the time took by your code , you will have to insert the below snippet . For more information, you can go to the msdn link. /* importing the lib*/ using System.Diagnostics; /* creating the object of Stopwatch, where we can it to calculate the time*/ Stopwatch stopwatch = new Stopwatch (); stopwatch.Start();// Start the time stopwatch.Stop();// Stop the time TimeSpan ts = stopwatch.Elapsed;             long elapsedMilliseconds;             elapsedMilliseconds = stopwatch.ElapsedMilliseconds; /*Conversion of elapsed time to milliseconds. We can even convert it in to minutes or hours. For more information please check the below link to msdn*/ Console .WriteLine( "RunTime " + elapsedMilliseconds); Msdn Link