Category: c-sharp-code-examples

Data structures and code examples in C# programming language

Configuration Manager for SqlConnection

Reading Time: 1 minuteIn 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: 1 minuteIn 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: 1 minuteThis 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: ,

189-binary-search-tree-height-in-c

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,

188-binary-search-tree-implementation-in-c

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

182-time-complexity-and-big-o-notation

Reading Time: 2 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

180-linq-orderby-operator-and-lambda-expression-in-c

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

179-linq-restriction-operator-where-clause-indexed-in-c

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[]

178-linq-let-clause-let-keyword-using-c

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

177-linq-join-operator-in-c

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,

176-linq-set-operators-union-intersection-except-in-c

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

175-linq-example-mapping-one-array-to-another-in-c

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

173-making-stack-data-structure-with-list-in-c

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

171-count-distinct-elements-in-array-in-c

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

161-order-by-in-linq-query-and-linq-query-on-xml-document-c

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

160-send-json-text-over-http-in-c

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

159-read-xml-file-and-write-xml-file-in-c

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.

158-convert-json-to-xml-and-convert-xml-document-to-text-in-c

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

156-open-excel-doc-in-c

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: 1 minuteIn 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”.

154-convert-xml-to-json-in-c

Reading Time: 2 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

153-htmlagilitypack-selectsinglenode-vs-descendantnodes-in-c

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

151-open-word-doc-in-c

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 microsoft.office.interop.word.dll. So you need to download this dll file first. Next step is to add

147-c-sharp-how-to-make-a-dll-file

Reading Time: 2 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

146-c-sharp-program-for-permutation.html

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

145-c-sharp-parallel-threading-example

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

144-c-sharp-md5-hash-code-example

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

142-c-sharp-basic-string-methods-and-parse-numberstyles

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

141-c-sharp-crc-computation

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

140-c-sharp-event-handler-for-serial-communication-with-delegate-method

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

139-c-sharp-crc-code-for-modbus-rtu

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

138-c-sharp-crc-checksum-code-for-binary-files

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

137-c-sharp-odbc-connection

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

136-c-sharp-binary-read-and-binary-write

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

134-c-sharp-timer-and-serial-communication-example

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

133-c-sharp-mdi-application

Reading Time: 2 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

124-c-sharp-sql-like-command-for-search

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

123-c-sharp-sql-delete-command

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

122-c-sharp-sql-update-command

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()

121-c-sharp-sql-retrieve-data

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()

120-c-sharp-sql-insert-command

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 = ”

119-c-sharp-sqlconnection-and-sqlcommand-class

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

118-c-sharp-sql-database-connection

Reading Time: 1 minuteThis 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

117-c-sharp-tab-control

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

116–c-sharp-menustrip-and-openfiledialog-example

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

115-c-sharp-listbox-and-combobox-example

Reading Time: 2 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

114-c-sharp-file-handling

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)

113-c-sharp-hashtable-example

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

112-c-sharp-arraylist-example

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

111-c-sharp-class-for-crc-checksum-calculator

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

110-c-sharp-array-and-multidimensional-array-example

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!”

109-c-sharp-class-example-constructor

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

Top