Category: c-sharp-code-examples

Data structures and code examples in C# programming language

Configuration Manager for SqlConnection

Reading Time: 2 minutesIn this tutorial, it is explained how to use Configuration Manager for SqlConnection. Firstly, we need to add Configuration Manager as reference to the project. Choose System.Configuration Use namespace using System.Configuration; Then we are ready to use Configuration Manager for

Tagged with: ,

Entity Framework Code First Example

Reading Time: 3 minutesIn this tutorial, Entity Framework code first example is given. We will continue from previous post and heaving code already we will make the database. This is actually the point of Entity Framework code first concept: to have classes in

Tagged with:

Basic Entity Framework C#

Reading Time: 2 minutesIn this tutorial, Basic Entity Framework C# is explained with no database, code first. To make it simple, we will prepare it as console application. We need to add reference from NuGet package. So, click on References folder and Manage

Tagged with: ,

Basic WCF Service C#

Reading Time: 2 minutesThis post explains Basic WCF Service C#. Let’s start with WCF service library Then, to existing solution add new project Choose Windows forms application Now, we need to add reference to this new Windows forms application Choose in Projects ->

Tagged with:

backgroundWorker Code Example for Cancel Button in C#

Reading Time: 2 minutesIn C#, backgroundWorker object is used to make program cancellation without blocking UI. In here, we will make Code Example for Cancel Button in C#. In your windows forms application, use backgroundWorker object. Once when backgroundWorker component is added to

Tagged with: ,

Biginteger class C#

Reading Time: 1 minuteBiginteger class C# allows us to handle really large integer numbers, much larger then standard int. In order to use it, namespace System.Numerics has  to be included in the current project. In order to include namespace System.Numerics, just click as

Tagged with: , ,

Noise Generator in C#

Reading Time: 2 minutesActually random generator in C# is so good, that we don’t need additional algorithms to make noise generator in C#. On the other hand, it is useful to have efficient algorithm to generate noise, not necessarily in visual studio. In

Tagged with:

FinancialFormula Forecasting in MSChart C#

Reading Time: 2 minutesMSChart control is very powerful tool for data display and analyzing. One very powerful feature is FinancialFormula Forecasting in MSChart C#. We can, for example,  model data with Linear Regression by using FinancialFormula Forecasting. Here is an example: chart1.Series.Clear(); chart1.Series.Add(“Over

Tagged with: ,

Convert Arraylist Into Array in C#

Reading Time: 1 minuteWhy and when we need to convert arraylist into array in C#? If we have some data in RAM memory (arraylists) and want to sort the data in one arraylist by another arraylist (they have to be the same size),

Tagged with: ,

Using mschart C#

Reading Time: 2 minutesThere are variety of possibilities to display data graphically if we know how to use ms chart control in C#. So using mschart in C# will be explained with mschart example in C#. We need to include namespace for using

Tagged with: , ,

Convert Datetime to int c#

Reading Time: 1 minuteWe need to convert datetime to int in c# for example when we want to add new series of the data in chart control to previously added series of data where x axis was given in datetime format. If in

Tagged with:

Keyword out and code examples in C#

Reading Time: 1 minuteBoth keyword ref and keyword out in C# change the behavior of called method parameters. The difference is that the parameter passed as ref must be initialized before passing to the method. Parameter passed with keyword out in C# needs

Tagged with: ,

Open Macro in Excel 2013

Reading Time: 1 minuteThis is just an brief instruction how to open macro in excel 2013. First go to File→Options Then click on Customize Ribbon and enable Developer option. Now developer option will appear in the excel 2013 toolbar. Then we can reduce

Tagged with: ,

Keyword ref and code examples in C#

Reading Time: 1 minuteKeyword ref in C# forces an argument to be passed to the method to be passed by reference. It means that argument must be initialized before it is passed to the method. It also means that any change in the

Least common multiple in C#

Reading Time: 1 minuteCalculate least common multiple Least common multiple is the smallest positive integer that is divisible by both a and b. Now, if we know greatest common divisor of two positive, greater then zero integers a and b, we can calculate

Greatest common divisor code in C#

Reading Time: 1 minuteGreatest common divisor of two positive integer numbers N and M, both larger then zero is the largest integer R that divide both N and M. If for given N and M, greatest common divisor is 1, we say that

Tagged with: ,


Reading Time: 3 minutesFirst functionality of methods in binary search tree class is to count nodes of binary search tree recursively and to measure binary search tree height. Taking the data from previous example, and starting with following array { 53, 72, 14,


Reading Time: 2 minutesMajor property of the binary search tree is self-similarity (every leaf can be root for another smaller binary search tree). For that reason, in BST we can use recursive functions to find or count the data. Searching through BST is


Reading Time: 3 minutesTime complexity is the important term in sense of how many steps has to be taken in order to complete given algorithm. In order to describe complexity, big O notation is used. For example, linear complexity is expressed in big


Reading Time: 1 minuteLINQ OrderBy operator we use to sort data in LINQ query. With using lambda expression (=>) example is follows: int[] nums = new int[] { 0, 4, 2, 6, 3, 8, 3, 1 }; var result1 = nums.Where(n => n


Reading Time: 1 minuteLINQ restriction operator where is very useful when we want to filter or restrict some data. For example, if we want to filter all numbers higher or equal 3 by using LINQ restriction operator where here is an example: int[]


Reading Time: 1 minuteLINQ Let clause i.e. LINQ Let Keyword is a part of a query expression. Let clause allows LINQ query expression to be more readable and code to be shorter and easier to understand. Let also introduces variable that can be


Reading Time: 1 minuteIn this article, we have LINQ example of usage of join operator in C#. Perhaps, the easiest way to explain usage of join operator in C# is by example. In first example we have two arrays of double type. Now,


Reading Time: 1 minuteIf we want to use LINQ Set Operators in C#, then set operations such as union, intersection and except are really one line of code. Take a look at following examples. Intersection: int[] set1 = { 1, 2, 3, 5


Reading Time: 1 minuteIf we have an array of strings, with people names for example and would like to display them in some arbitrary order given in another array of integers, we can manipulate two arrays with LINQ in C#. Here is LINQ


Reading Time: 1 minuteStack data structure is a LIFO collection and is a generic type. It exist in C#, and it is defined in namespace: using System.Collections.Generic; It can be declared as follows: Stack<int> stack = new Stack<int>(); and as any stack it


Reading Time: 1 minuteIf we use LINQ query, we can count distinct elements in array in C# by following command: int[] a = { 0,0,1,7,17,1,2,2,3 }; int count = a.Distinct().Count(); The other way to count distinct elements in array in C# if array


Reading Time: 4 minutesLINQ in C# stands for Language Integrated Query. It is brilliant programming concept implemented in C# programming language to virtualizes underlying data source. This means, that for example we can apply linq query on XML document in C# as easy


Reading Time: 2 minutesSending data over http in C# is easy if we use HttpWebRequest and WebResponse classes. Once when we have our data in json format ready for sending, we can send json text over http in C#. We need to make


Reading Time: 2 minutesTo read XML file in C# or to write XML file in C# it is best to use System.Xml namespace. Namespace System.Xml.Linq; is also useful for this purpose. Now, to write XML file, we need one object of XmlWriterSettings class.


Reading Time: 1 minuteIn previous article we discussed how to convert XML data to json format. Converting in opposite direction is as just as simple. So to convert json to XML in C# let’s assume that we have data in json format are


Reading Time: 3 minutesTo get data from Excel in C# or to insert data into Excel in C#, we need to open Excel doc in C# first. There are two ways to access Excel data in C#, either To use Microsoft.Office.Interop.Excel.dll namespace or

Creating ms access database from excel worksheet

Reading Time: 2 minutesIn this tutorial it is explained how to create MS Access database from excel worksheet. First, we need to create new MS Access database. We need to give full path and type name of new database. And then, click “Create”.


Reading Time: 3 minutesIf XML data are small in structure, there is a way to convert XML to json in C# by knowing how to serialize dictionary to json in C#. We need to use using System.Web.Script.Serialization; namespace. It has to be added


Reading Time: 2 minutesScraping data in C# is easy using HtmlAgilityPack. In this short tutorial we will focus on how to select Nodes and Subnodes with HtmlAgilityPack. In this project, we need to download htmlagilitypack.dll. Next step is to add reference, like for


Reading Time: 1 minuteTo handle MS Office documents from C# it is essential to add assembly to your C# project. Reference for handling MS Office documents is in So you need to download this dll file first. Next step is to add


Reading Time: 3 minutesIn order to make a dll file from C#, first step is to start creating class library .dll project (see figure bellow). Rest of the job is similar to writing down any other C# class, but we need to take


Reading Time: 1 minuteMaking all permutation of the given set of characters is most time effective if it is done with Heap’s algorithm. C# program for permutation with recursive function call in according to Heap’s algorithm is presented. First thing that we need


Reading Time: 1 minuteTo use paralleling of the treads in C#, we need to use System.Threading.Tasks; namespace. There are several ways to parallelize threads, including usage of PLINQ. In here, for educational purpose most simple c# parallel threading example as console application is


Reading Time: 1 minuteC# md5 hash code can be easily obtained by using following namespaces: System.Security.Cryptography; System.Globalization; System.IO; If we want to calculate md5 hash that is 32 characters long string of any input for simple string, we can do it with just


Reading Time: 2 minutesKnowing C# basic string methods can be very useful and time saving. In this article, we will use some of C# basic string methods in order to appropriately parse large string for crc bytes calculus. For that purpose, it is


Reading Time: 2 minutesRead also C# crc checksum class C# crc checksum code for binary files C# crc checksum code for MODBUS RTU CRC computation is the method for error detection in sending/receiving binary data. It is different from error auto correction, but


Reading Time: 3 minutesC# event handler for serial communication is equivalent to interrupt concept. When data arrives from outside world into serial port buffer, interrupt signal is generated. To make application that uses serial communication with event handler in c#, it is practical


Reading Time: 3 minutesModbus is serial communication protocol. It is widely spread in industry and mainly is used to connect master communication device (usually PC) to remote terminal unit (RTU). Information package from master to slave device in MODBUS protocol has following format


Reading Time: 2 minutesA cyclic redundancy check (CRC) is an error-detecting code. In this article, C# crc checksum code for binary files is given. For 16 bit CRC calculus for binary files, first step is to have all of the data (all of


Reading Time: 1 minuteWhen there is a need to program databases in C# we can access given database either by Using SqlConnection and SqlCommand class or by using ODBC connection. ODBC – Open Database Connectivity is Microsoft interface for accessing database management systems


Reading Time: 2 minutesIn order to access binary file from C# it is comfortable to use OpenFileDialog class. To handle binary, either to read binary file or to write to binary file in C#, we need to use System.IO namespace. Inside System.IO namespace


Reading Time: 2 minutesC sharp application uses timer whenever is necessary for application to regularly control something in equal time intervals, from reading the system clock to communicate with firmware via serial communication. Timer tick function must do the regular task every time


Reading Time: 3 minutesMaking Multiple-Document Interface (MDI) in C# is your next big step towards professional programming. Any good windows forms application has to have multi functionality, i.e. many child forms that should be accessible from parent form by using menu. Actually, every


Reading Time: 1 minuteEasier way to connect to database is C# odbc conncetion to database In this tutorial c sharp SQL like command for search is explained, i.e. how to search similar data with SQL query command like from C#. We continue from


Reading Time: 1 minuteEasier way to connect to database is C# odbc conncetion to database In this tutorial c sharp SQL delete command is explained, i.e. how to delete data with SQL query from C#. We continue from initial database tutorial. C sharp


Reading Time: 1 minuteEasier way to connect to database is C# odbc conncetion to database In this tutorial c sharp SQL retrieve data is explained, i.e. to retrieve data with SQL query from C#. We continue from initial database tutorial. private void clear_all_fields()


Reading Time: 1 minuteEasier way to connect to database is C# odbc conncetion to database In this tutorial c sharp SQL retrieve data is explained, i.e. to retrieve data with SQL query from C#. We continue from initial database tutorial. private void clear_all_fields()


Reading Time: 2 minutesEasier way to connect to database is C# odbc conncetion to database In this tutorial C Sharp SQL Insert command is explained, i.e. how SQL Insert Command is implemented in C#. We continue from previous example. string str_sql_insert = ”


Reading Time: 2 minutesEasier way to access database from C# is to Connect to database via odbc connection If we connect to databases in C# directly, two additional namespaces are necessary: using System.Data.SqlClient; using System.Collections; In our C# program, we need to use


Reading Time: 2 minutesThis example is for Microsoft SQL Server 2008 database connection. First step is to make right click in Solution Explorer and Add->New Item… Then, choose Service-based Database. Choose a Database Model. Choose your Database Object. Once when Data Connection is


Reading Time: 1 minuteC sharp tab control is control from TabControl Class. C sharp tab control is the part of C# Containers Toolbox. Once when it is added to Windows form, additional tabs can be added as collection. We can add two more


Reading Time: 2 minutesC sharp menuStrip and c sharp OpenFileDialog are visual controls in C# for adding application menu items and opening files, respectively. C sharp menuStrip is the part of C# Menus & Toolbars Toolbox. C sharp OpenFileDialog is the part of


Reading Time: 3 minutesC sharp listbox and c sharp combobox are among most popular visual controls in C# windows forms applications. They are similar and they have similar proporties and methods. In this example, we will prepare one form with one listbox, one


Reading Time: 1 minuteC sharp file I/O methods are in namespace called System.IO. C sharp file handling implies files listing, files and folders creation, writing down and reading from file. Here is the code for listing folder: using System.IO; static void Main(string[] args)


Reading Time: 1 minuteC sharp hashtable is very useful data structure in C#. Actually, it is kind of arraylist with two-dimensional Items. With this two-dimensional feature it can be very easy to define look-up table for different type of data such as string


Reading Time: 2 minutesC sharp arraylist are particularly useful in: Collecting ID from the database C sharp arraylist is different from list as data structure in good old fashion C. List is a data structure where nodes can’t be accessed by index, but


Reading Time: 2 minutesAbout CRC checksum calculus, you can also read: C# crc checksum code for CRC-16-ANSI, CRC-CCITT, CRC-XMODEM C# crc checksum code for binary files C# crc checksum code for MODBUS RTU A cyclic redundancy check (CRC) is an error-detecting code. It


Reading Time: 2 minutesC sharp array is different in defining from array in C programming language. Array in C program would go like this: int a[10]; C sharp array example in here would be used together with for loop to write “Hello World!”


Reading Time: 2 minutesC Sharp is object oriented programming language. It means that programmers should be object oriented in thinking while writing down code in C#. For simplicity, and completeness of this tutorial, our first c sharp class example will be given as