Archive | C# Language RSS feed for this section

C# Strings – Getting Started with Strings

Working with strings is a very common task for most C# developers. The .NET Framework offers good variety of tools for working with strings, but care must be taken as there are several gotchas to trip up the beginner. The first thing to note about strings in .NET is that they are Reference Types. Reference […]

Read more

Passing Parameters in C#

There are three primary methods of passing parameters to C# methods: Regular Parameter Passing This is passing parameters with no modifying keywords : void MyMethod(Student studentObj, int aNumber) { aNumber += 5; studentObj.Name = “Jon”; } In the above example MyMethod takes two parameters – a Student object and an Integer. Note the difference between […]

Read more

C# Custom Number Formatting

Very often the inbuilt numerical formatting in C# will be insufficent and you will want to apply the custom formatting for your numbers. The String.Format method is very flexible and can be used to apply custom formatting rules. The # character informs the Format method how to format the numerical value, for example to forma […]

Read more

Formatting Numerical Data in C#

C# ships with several inbuilt formatting specifies which can be used to quickly format a number, for example the ‘c’ specifier will format the number as a currency: double dbl1 = 9999999.9999999; outputStr = string.Format(“This is the currency format {0:c}”, dbl1); This will output the numerical value as a currency based on the user’s current […]

Read more

Strings are Immutable!

Working with strings is a common task in most apps but there are several ‘gotchas’ that can arise due to the immutable nature of a  String in .NET. Immutable simply means that once created it doesn’t change. This seems strange at first since the value of a string variable can indeed be altered : string […]

Read more

Using the .NET Stopwatch class to Profile Your Code

The Stopwatch class in the System.Diagnostics namespace can be used a as a basic tool to profile blocks of .NET code. System.Diagnostics.Stopwatch timerObj = new System.Diagnostics.Stopwatch(); timer.Start(); Decimal totalDec = 0; int limit = 1000000; for (int i = 0; i < limit; ++i) { totalDec = totalDec + (Decimal)Math.Sqrt(i); } timerObj.Stop(); Console.WriteLine(“Sum of square […]

Read more

Ensure a .NET App Works in Both 32 bit and 64 bit Environments

With 64 bit versions of Windows fast becoming the standard it is imperative that an app work in both 32 bit and 64 bit environments. This done using a basic configuration setting, with a few caveats of course. In Visual Studio in the project’s build options you can select the app’s Platform target, which can […]

Read more

Call C++ Functions in a DLL using C#

P/Invoke is an efficient method for calling native code functions in an unmanaged DLL. The below code samples show the source code a native code library which defines a very simple C++ function which accepts a char* argument. MyLib.h __declspec(dllexport) int Hello(char* pszBuffer, int nLengthObj); MyLib.cpp #include “stdafx.h” #include “MyLib.h” #include __declspec(dllexport) int SayHello(char* pszBuffer, […]

Read more

Measure Memory Usage of .NET Applications

There are two main methods for measuring the memory usage of a .NET application, using the GC class or using System.Diagnostics Retrieve Your App’s Memory Utilization using the GC class The .NET Framework’s GC class contains many useful memory-related methods, including GetTotalMemory(), which returns the amount of memory the garbage collector believes is allocated to […]

Read more

C# Dynamic Binding – Language Binding

Language binding is a form of dynamic binding which occurs when a dynamic object does not implement IDynamicMetaObjectProvider. Language binding comes in handy when working around imperfectly designed types or the inherent limitations in the .NET type system. A common problem is that when using numeric types is they have no common interface. Using dynamic […]

Read more