Category: c-sharp-code-examples

Data structures and code examples in C# programming language

Configuration Manager for SqlConnection

In 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

Posted in c-sharp-code-examples Tagged with: ,

Entity Framework Code First Example

In 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

Posted in c-sharp-code-examples Tagged with:

Basic Entity Framework C#

In 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

Posted in c-sharp-code-examples Tagged with: ,

Basic WCF Service C#

This 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 ->

Posted in c-sharp-code-examples Tagged with:

backgroundWorker Code Example for Cancel Button in C#

In 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

Posted in c-sharp-code-examples Tagged with: ,

Biginteger class C#

Biginteger 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

Posted in c-sharp-code-examples Tagged with: , ,

Noise Generator in C#

Actually 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

Posted in c-sharp-code-examples Tagged with:

FinancialFormula Forecasting in MSChart C#

MSChart 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

Posted in c-sharp-code-examples Tagged with: ,

Convert Arraylist Into Array in C#

Why 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),

Posted in c-sharp-code-examples Tagged with: ,

Using mschart C#

There 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

Posted in c-sharp-code-examples Tagged with: , ,

Convert Datetime to int c#

We 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

Posted in c-sharp-code-examples Tagged with:

Keyword out and code examples in C#

Both 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

Posted in c-sharp-code-examples Tagged with: ,

Open Macro in Excel 2013

This 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

Posted in c-sharp-code-examples Tagged with: ,

Keyword ref and code examples in C#

Keyword 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

Posted in c-sharp-code-examples

Least common multiple in C#

Calculate 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

Posted in c-sharp-code-examples

Greatest common divisor code in C#

Greatest 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

Posted in c-sharp-code-examples Tagged with: ,

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

First 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,

Posted in c-sharp-code-examples

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

Major 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

Posted in c-sharp-code-examples

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

Time 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

Posted in c-sharp-code-examples

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

LINQ 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

Posted in c-sharp-code-examples

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

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

Posted in c-sharp-code-examples

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

LINQ 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

Posted in c-sharp-code-examples

177-linq-join-operator-in-c

In 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,

Posted in c-sharp-code-examples

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

If 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

Posted in c-sharp-code-examples

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

If 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

Posted in c-sharp-code-examples

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

Stack 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

Posted in c-sharp-code-examples

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

If 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

Posted in c-sharp-code-examples

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

LINQ 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

Posted in c-sharp-code-examples

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

Sending 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

Posted in c-sharp-code-examples

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

To 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.

Posted in c-sharp-code-examples

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

In 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

Posted in c-sharp-code-examples

156-open-excel-doc-in-c

To 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

Posted in c-sharp-code-examples

Creating ms access database from excel worksheet

In 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”.

Posted in c-sharp-code-examples

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

If 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

Posted in c-sharp-code-examples

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

Scraping 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

Posted in c-sharp-code-examples

151-open-word-doc-in-c

To 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

Posted in c-sharp-code-examples

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

In 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

Posted in c-sharp-code-examples

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

Making 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

Posted in c-sharp-code-examples

145-c-sharp-parallel-threading-example

To 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

Posted in c-sharp-code-examples

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

C# 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

Posted in c-sharp-code-examples

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

Knowing 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

Posted in c-sharp-code-examples

141-c-sharp-crc-computation

Read 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

Posted in c-sharp-code-examples

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

C# 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

Posted in c-sharp-code-examples

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

Modbus 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

Posted in c-sharp-code-examples

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

A 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

Posted in c-sharp-code-examples

137-c-sharp-odbc-connection

When 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

Posted in c-sharp-code-examples

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

In 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

Posted in c-sharp-code-examples

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

C 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

Posted in c-sharp-code-examples

133-c-sharp-mdi-application

Making 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

Posted in c-sharp-code-examples

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

Easier 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

Posted in c-sharp-code-examples

123-c-sharp-sql-delete-command

Easier 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

Posted in c-sharp-code-examples

122-c-sharp-sql-update-command

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

Posted in c-sharp-code-examples

121-c-sharp-sql-retrieve-data

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

Posted in c-sharp-code-examples

120-c-sharp-sql-insert-command

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

Posted in c-sharp-code-examples

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

Easier 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

Posted in c-sharp-code-examples

118-c-sharp-sql-database-connection

This 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

Posted in c-sharp-code-examples

117-c-sharp-tab-control

C 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

Posted in c-sharp-code-examples

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

C 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

Posted in c-sharp-code-examples

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

C 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

Posted in c-sharp-code-examples

114-c-sharp-file-handling

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

Posted in c-sharp-code-examples

113-c-sharp-hashtable-example

C 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

Posted in c-sharp-code-examples

112-c-sharp-arraylist-example

C 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

Posted in c-sharp-code-examples

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

About 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

Posted in c-sharp-code-examples

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

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

Posted in c-sharp-code-examples

109-c-sharp-class-example-constructor

C 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

Posted in c-sharp-code-examples