Tag Archives: c#

Using Parallel LINQ in C#

The snippets below illustrate Parallel LINQ in C# by crawling a web page in parallel. The Crawler class is responsible for crawling a web page (only one level deep) and returning a list of documents. The Document class represents an HTML document.

Posted in C# | Also tagged , , , , , , , , , | Leave a comment

GZip compression and decompression in C#

The snippets below illustrate GZip compression and decompression using the GZipStream classes.

Posted in C# | Also tagged , , , , , , , , , , | Leave a comment

Parsing (X)HTML into a DOM tree in C#

As a quick alternative to the Html Agility Pack, HTML can be parsed into a DOM tree using the built-in XmlDocument class. The following snippets illustrate HTML parsing in C#.

Posted in C# | Also tagged , , , , , , , , , | Leave a comment

Alphanumeric/natural sorting in C# using IComparer

These snippets illustrate the natural sorting order. Suppose you’re using numbers within your file names. For example: file1.png file2.png … file10.png file11.png file12.png You’d expect to see them in the above order. However, ¬†they are (by default) sorted as follows. file1.png file10.png file11.png file12.png file2.png …

Posted in C# | Also tagged , , , , , , , | Leave a comment

(C#) C# LINQ & Extension method continued

Also extended C# LINQ and Extension method (simple) to include the last (MAX) Date Created property, added a new Folder class (model) with properties and included them in a List. Also, amended the LINQ statement to get the MAX(Date Created) .

Posted in C# | Also tagged , , , , | Leave a comment

(C#) Pull Image into Byte Array from a URL

This snippet pulls down an image from a URL into a byte[]. From there you can re-size or save. Don’t forget the necessary try – catch.

Posted in C# | Also tagged , , , , , , , | Leave a comment

Primality testing with Fermat’s little theorem and Miller-Rabin in C#

Prime numbers have many applications. For example, in cryptography. The RSA algorithm for public-key cryptography uses large prime numbers for generating keys. Quote: The RSA algorithm works as follows: take two large primes, p and q, and compute their product n = pq; n is called the modulus. Choose a number, e, less than n [...]

Posted in C# | Also tagged , , , , , , , , , , , , , , , , | 2 Comments

(C#) Download text or bytes via HTTP

Downloads contents as a byte array or string, depending on need.

Posted in C# | Also tagged , , , , , , , , , | Leave a comment

C# code execution at runtime with the Microsoft Roslyn CSharp ScriptEngine class

Microsoft has released the Roslyn CTP which allows developers to use the Visual Basic and C# compilers as APIs. The snippet below can be used to execute C# code at runtime.

Posted in C# | Also tagged , , , , , , | Leave a comment

Huffman coding in C#

Huffman coding is an encoding algorithm used for data compression. So called Huffman trees are used for encoding and decoding of data. The leaves of these trees contain the symbols and their frequencies. Each parent node contains the sum of the frequencies of its children.

Posted in C# | Also tagged , , , , , , , , | 11 Comments