What is the difference between ToUpper() and ToUpperInvariant() methods of String class in .Net.

The String class has two methods called ToUpper() and ToUpperInvariant().  What are their differences and when to use which one?

ToUpper() method convert all chars to uppercase by leveraging casing conventions of “current culture”.

ToUpperInvariant() methods does the same job as ToUpper() by leveraging casing convention of “invariant culture”. So it produces the same result for any system regardless of the user’s current culture.  Therefore, when comparing the system identifiers, i.e. file names, paths, etc., normalizing the identifiers with ToUpperInvariant() is the right choice.

So the next question is … What culture does ToUpperInvariant() uses?

Below are the decompiled code of the methods inside System.String class.

public string ToUpper()
{
      return this.ToUpper(CultureInfo.CurrentCulture);
}
public string ToUpperInvariant()
{
      return this.ToUpper(CultureInfo.InvariantCulture);
}

By the way… What is the InvariantCulture?

The short answer is it is a common culture that can be used regardless of user’s country and language.

The detailed answer is clearly explained in “Using the InvariantCulture Property” and “Performing Culture-Insensitive String Operations”.

To sum up, as a general best practice, ToUpper() should be called when handling user generated strings while ToUpperInvariant should be called when handling system resources such as filenames and uri’s.

Advertisements

Visual guide for the algorithm “Common Subsequence Count”

Cees Elzinga, Sven Rahmann and Hui Wang have perfectly explained several algorithms on string sequences on their paper , namely Algorithms for Subsequence Combinatorics.

Of those algorithms, I analysed the algorithm  “Counting Common Subsequence” to attack the problem Square Subsequences in HackerRank.

In order to understard the algorithm better, I prepared a visual representation of the algorithm steps in this powerpoint file. It may be a helpful visual guide for those who are struggling with the details of the algorithms.  I would like to, also, take this opportunity to thank each and everyone of the authors of the paper.

Recipe: How to make use of intellisense feature when coding JavaScript in Visual Studio 2010

Motivation

You have included a JavaScript library and you want to make use of intellisense while coding JavaScript in Visual Studio but you couldn’t find the correct way for it.

Recipe

Ingredients

  • 1 JavaScript library to include (i.e. jslinq.js)
  • 1 -vsdoc.js file of library you want to include (i.e. jslinq-vsdoc.js)

Directions

  • Include the -vsdoc.js file. You don’t need to include it in the same folder where your actual .js file is located.  However, it is more practical to keep them as close as possible for the ease of maintenance, code navigation and delightful presentation 🙂

image

  • Put the following code at the top of the JavaScript file (i.e. map.js) where you call functions of the included library (i.e. JSLINQ.js).
/// <reference path="~/Scripts/jslinq/JSLINQ-vsdoc.js" />

Tip of the recipe

    • This “<reference … /> codes” must be the first code in you calling JavaScript file (i.e. map.js)
    • The referenced path must be the path of –vsdoc.js file. Not the path of the actual js file !

 

  • Create an object of the library.

image

Tip of the recipe

  • If you create in the wrong way, although you can still use the object as expected, the intellisense won’t work.
  • Now you can make use of intellisense while you code JavaScript.

image

  • That’s it enjoy your code 🙂

The nitty gritty of primitive data types in .NET

The C# Language Specification 4.0 indicates that

C# provides a set of predefined struct types called the simple types. The simple types are identified through reserved words, but these reserved words are simply aliases for predefined struct types in the System namespace, as described in the table below.

image

So far so good.

However, when we uncover the struct definition of Int32 by means of disassembler we quickly figure out that it has a field of the type int, namely m_value.
namespace System
{
    public struct Int32
    {
        internal int m_value;
    }

}
So this seems to be a bizarre cyclic definition.  In addition, the field m_value is never accessed neither get nor set accessors.

The question is how this happens and how exactly the primitive types store their values? 

Answer:

(Below is just my interpretation for this question after digging some relevant specification documents.  The exact answer may differ to some degree)

Let’s analyze the CIL code that is generated by the following C# code.

static int Main(string[] args)
{
            //case 1: ordinary parameter names
            int paramInt = 0xABCD;
            Int32 paramInt32 = 0x1234;

            //case 2: parameter names are identical to keywords
            int int32 = 0xDCBA;
            Int32 Int32 = 0x4321;

            //all parameters are used just to suppress compiler optimization
            return paramInt + paramInt32 + int32 + Int32;
}

image

What can be seen in CIL code that both int’s and Int32’s are converted to clr type int32 (See Table I.1 in Built-in value and reference types header of CLI Specification 6th Edition).

So when compiling, the conversion is not

                                 int->Int32->int32

but rather it is

                                 int->int32

                                 Int32->int32

.

So the cycle is broken by converting simple types directly to the CLI types when compiling.  The CLI code of System.Int32 struct is generated with a field of CLI type int32 which is perfectly valid.

image

Since the 4-byte field m_value is the first and only field, m_value is aligned directly with the whole value of the 4-byte struct Int32 in the corresponding memory location.  That’s why there is no get/set accessors of m_value in the struct Int32. The same explanation can be applied to all other primitive data types in .NET.

Acknowledgements: I would like to thank to the members of Joel on Software Discussion Community for sharing their knowledge under this post.