C# Programming
String Manipulation
<string type>.IndexOf
- Searches for a particular character within a string, and returns the position of the first instance found. Can specify where in the string to start looking, as well as how many characters to look through
Convert.To<type>
- Converts back and forth from many standard types, including integers and strings
There is a great cheat sheet by John Sheehan which explains string manipulation in the .NET language. Unfortunately, it can no longer be found on his site, but you can still download this locally cached version.
Removing Characters From A String
<string type>.Remove
- Removes characters from a string, starting at a specified location. Note that this creates a new string with chosen characters removed, it DOES NOT touch the original string! This is a common mistake for people to make, and results in nothing happening to the string.
Converting To Hex
Use the Convert.ToString()
class to convert to a hexadecimal string. The case of the format specifier (i.e. x
or X
), determines whether the alphabetical characters in the hex output (A-F) will be in lower-case or in upper-case.
Populating A ComboBox From An Enumeration
Populating a combo box from an enumeration can save heaps of typing and manual work to enter each item individually. It is done easily with the one line of code below:
You can also use the actual value of an Enum
rather than it’s name by casting it to an int
.
Iterating Through An Enumeration
Use the following code:
Running .NET On Non-Windows Platforms
There is a popular “cross platform, open source, .NET development framework” called Mono. It is an open source implementation of the .NET framework which can run on a number of devices including Linus, Mac OS, Windows (of course), and even the RaspberryPi!
Signal Processing
The Math.Net project contains the “Neodym” library for signal processing in C#. See the Signal Processing page for more information.
Rounding
The Math library (Math.<some function>
) supports basic rounding. However, here is a trick to round to any specified value. You basically divide the number by the precision (the number to want to round to, e.g. 0.1, 0.25, 5 e.t.c), then use standard rounding on the result, and lastly multiply the rounded number by the precision to get the answer.
You can also preform variable precision rounding with an offset.
Profiling And Performance Tuning
To programs to do this are SlimTune and EQATEC.
Command-Line Parsers
Command-line parsers are a good way for user-program interaction and well as computer-computer communication. There are quite a few free and opensource C# Command-Line Parsers out there. Their core functionality is similar to the C-based linux getopt() command, but incorporate the powerful functionality of the .NET language. Most use inline lambda notation to create the equivalent of call-back functions when registering the parameters.
Command Line Parser Library
Author: GSSCoder Author’s Link: https://github.com/gsscoder/commandline CodePlex: http://commandline.codeplex.com/ NuGet: “install-package CommandLineParser” (latest stable), “install-package CommandLineParser -pre” (latest release)
This library has had a continuously updated API since 2005. Parses commands in the “—age 50” style. Requires the parameters to registered with a custom class at build time.
Does not support an initial keyword that determines the nature of the following parameters (similar to calling a particular program).
cmdline
Author: Ron Jacobs Author’s Link: http://code.msdn.microsoft.com/Command-Line-Parser-Library-a8ba828a NuGet: “install-package cmdline”
NDesk.Options
Author: NDesk Author’s Link: NuGet:
This one can support multiple commands, with the additional library ManyConsole. NDesk.Options replaced the now obsolete Mono.Options library, and it is include in the Mono 2.0 package.
ManyConsole
Author: https://github.com/fschwiet Author’s Link: https://github.com/fschwiet/ManyConsole NuGet: install-package ManyConsole
This library supports multiple commands, which is useful if you want to use the command-line style for communication between two computers/microcontrollers. It uses the NDesk.Options library. In my opinion, ManyConsole is hard to use, and requires all the commands/parameters to be created at compile time as classes.
Microsoft Office Program Manipulation With C#
You can manipulate Microsoft Office applications quite easily within the .NET framework (which includes C#). See the following page I wrote for more information.
=> Windows Office Application Interface
Installing Third-Party Libraries
I recommend a package manager like NuGet is used to install third-party libraries/packages whenever possible. NuGet allows you to select a pre-compiled third-part library from a large online database and then install it into a Visual Studio project at the click of a button. It also removes a library just as quickly. The image below shows the NuGet package management screen
Other Resources
If you have the money, you can buy the MS Word component Spire.Doc from E-IceBlue. It provides an API to easily create and manipulate Word documents in an easier manner than with Microsoft’s standard library.