Posts

Showing posts with the label C#

Cross-Platform C# Development

Imagine you have an idea for a great application that will make millions of users happy. But what users - Windows users, Mac users, Linux users, iOS users, Android users, Chrome OS users? You should pick one or should you? We all know about the Mono project and Xamarin that let you write C# code that runs on different platforms, but this is not enough, because all these platforms have different APIs the C# code is bound to, so you will have to write different C# code for each platform. Fortunately, a new solution has been presented recently that provides really cross-platform C# development, which means that you write your C# code once and it runs on all platforms with no modifications needed! Meet the Nevron Open Vision .

The Most Useful Visual Studio Keyboard Shortcuts

Microsoft Visual Studio is one of the best Integrated Development Environments (IDEs) available and as such it dramatically boosts the productivity of the programmers who use it. You can find many articles over the Internet that show you how to boost your productivity even more. One of the ways to do so is to take advantage of the keyboard shortcuts in Visual Studio. Unfortunately, most of the articles present hundreds of Visual Studio keyboard shortcuts, which is impossible for one to remember. Do you wonder which ones of them are the most useful and worth remembering? If yes, read on...

Colorize C# Code For Blog or Website

If you are running a C# related blog or website you know how important it is to have a reliable service to convert your C# code to HTML ready to be published online. Of course you can do this by copying your formatted C# source code from Visual Studio to Microsoft Word and then paste it from Word to your blog/website. Unfortunately, as always, the easy way is not the best way. Pasting your code from Word to your blog will produce a huge amount of unnecessary HTML code which will make your website heavy and it will load slow. There should be a better way to highlight the syntax of your C# code like Visual Studio does and insert it in your blog or website.

Make Color Lighter or Darker in C#

While developing the user interface of an application you may need to change the color of a specific element to draw user's attention. The most common technique to do so is to make the element's background or foreground color lighter or darker. The method I use simply applies a given correcttion factor in the interval [-1, 1] to the red, green and blue color components. Negative values produce darker colors and positive ones produce lighter colors.

Change DataGridView Cell Cursor

The .NET data grid view is a nice grid control which allows a lot of customization and can fit well in many scenarios. However, if you want to change the cursor of a specific data grid view cell when the mouse is over it (a.k.a. mouse hover), then you will have some hard time. Fortunately, there's a way to do this. You have to handle the CellMouseEnter and the CellMouseLeave events, changing the cursor of the data grid view to the desired one in the CellMouseEnter event handler and restoring it in the CellMouseLeave event handler.

C# Case Insensitive String Comparison

The .NET framework gives us two easy ways to check whether two strings are equal or not ignoring their case. The first way is to convert each string to lower case using the String.ToLower or the faster String.ToLowerInvariant method and then compare the strings with the "==" operator and the second way is to to use the String.Equals method specifying an ordinal ignore case string comparison type. Both methods will get the job done, but have you ever wondered which one is faster?

Arbitrary to Decimal Numeral System Conversion in C#

In the article Decimal to Arbitrary Numeral System Conversion in C# I've presented a function that converts a decimal number to a number in an arbitrary numeral system. All you had to do was to call the method passing the decimal number to convert and the radix (a.k.a. base) of the destination numeral system as arguments. Now I'm going to present you a solution to the opposite task - convert a number from an arbitrary to decimal numeral system. The presented implementation can convert any number in an arbitrary numeral system with radix from 2 to 36 to a decimal number.

Compare Algorithms Performance

Have you ever wondered if a given C# algorithm/code fragment is faster than another that does the same job? If the answer is yes, then continue reading this article as it will give you a facility to easily test both code fragments and determine the faster one. To compare the performance of 2 code fragments we need to execute them one by one and measure the time each of them has worked. Many developers use the DateTime.Now  property for this purpose, but it has one major flaw - it is not very accurate. The most precise method to measure elapsed time in the .NET framework is the Stopwatch class.

Measure C# Code Performance

Conscious developers always try to squeeze another millisecond or two of their code's execution time. But in order to know whether their code's performance has actually improved or not, they should be able to measure the the time their code has worked. Many developers use the DateTime.Now  property for this purpose, but it has one major flaw - it is not very accurate. The most precise method to measure elapsed time in the .NET framework is the Stopwatch class.

Get All Derived Types of a Class

A common requirement for all .NET module based applications is the ability to get all types that derive from a given base class. This task sounds easy at first, as the .NET Framework provides a convenient Type.IsSubclassOf method, but it is not! The problem arises when generics are involved, for example when you want to find all types that inherit from a given generic class in a specific assembly. In this case the Type.IsSubclassOf method won't work and will return false.  

Decimal to Arbitrary Numeral System Conversion in C#

Developers often need to convert decimal numbers to binary, octal, hexadecimal or another numeral system. As this is a common task, there are many examples over the Internet how to do it. You can easily find a lot of decimal to binary, decimal to octal, decimal to hexadecimal, etc. converters but it is hard to find a more generalized converter that can convert a decimal number to any other numeral system. That is what I'm going to show you in this article. The presented implementation can convert any decimal number to an arbitrary numeral system with radix from 2 to 36.

A Bit Matrix in C#

Bit matrices (a.k.a. 2D bit arrays) are the most memory efficient way to store two dimensional arrays of boolean values. A boolean value in .NET framework (i.e. value of type bool) uses 1 byte of memory, but a boolean value in the presented bit matrix data structure uses only 1 bit, which will result in 8 times memory usage reduction. This data structure can be used in many scenarios where huge arrays of boolean values need to be stored in memory such as matrix representation of large graphs (i.e. adjacency matrix graph representation), huge binary value tables, etc.