Vadim's Weblog

Never stop learning.

Capitalize only first character. (C#)

Posted by Vadim on October 9, 2013

Once in a while, you may need need to capitalize only the first character of a word. Like everything else in programming, there’s more than one way to solve this problem. In this post, I’m going to explore three different ways to capitalize the first letter of a word, and then let you know which one I prefer.

1. String manipulation solution.
private string UpperFirst(string text)
return char.ToUpper(text[0]) +
((text.Length > 1) ? text.Substring(1).ToLower() : string.Empty);


This solution is straightforward. First, capitalize the initial character of the text, and then append the rest. Next, we test if our text has more than 1 character. If this is text with multiple characters, we convert the rest of the text to lowercase, in case there are any uppercase letters.

2. LINQ solution.
private string UpperFirst(string format)
return format.First().ToString().ToUpper() +
String.Join("", format.Skip(1)).ToLower();


LINQ solution is also easy to follow and understand. We get the first character, and then we convert it to String (because the char type doesn’t have instance implementation of ToUpper()). Then, like in the previous solution, we append the rest of text.

3. TextInfo.ToTitleCase solution.
private string UpperFirst(string text)
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text.ToLower());


This solution is taking advantage of the ToTitleCase method. It looks simple, but it’s not exactly like our two previous solutions. The problem with this solution is that it’s going to capitalize every word in the text.  For example: if your text has the value “hello world”, the result of this solution will be “Hello World” and not “Hello world” as in the two earlier examples. If you need to capitalize every word in your text, use this solution. Otherwise, use one of the earlier solutions.

The best solution for me.

Personally, the 1st solution looks the best. There are two reasons why I think solution #1 is the best:

  1. I believe it’s easier to read, but readability is very subjective.
  2. It performs better. I iterated each solution 1,000,000 times, and the first solution performed the fastest.

Below is the table of my performance test, in case you’re interested. It shows the time in milliseconds for 1,000,000 iterations.

Solution Time
String manipulation







Please let me know if you can think up other implementations for capitalizing letters.

Technorati Tags: ,,,

Posted in C# | Tagged: , , , | 3 Comments »

Corrected problem with SystemWrapper in NuGet gallery.

Posted by Vadim on June 24, 2011

Yesterday, I told the world that SystemWrapper is in NuGet gallery.  However, I didn’t realized that I publish the wrong version that does absolutely nothing.

I just want to let you know that I re-publish the correct version and everything should work fine. 

I originally tested the package on my local machine but I’ve never tested the published one.  It taught me AGAIN that I should not to assume anything.

I hope you’ll forgive for this embarrassing, rookie mistake.

Posted in SystemWrapper | Tagged: | Leave a Comment »

SystemWrapper is in NuGet Gallery.

Posted by Vadim on June 23, 2011

In this short post I just want to let you know that SystemWrapper is in NuGet Gallery now.

I should’ve done it before a request on CodePlex.  But it’s better later than never.

Please go to NuGet Gallery and rate SystemWrapper.

Posted in SystemWrapper | Tagged: | Leave a Comment »

From hobbies to a professional.

Posted by Vadim on May 12, 2011

Most people have hobbies, at least I hope that most people have some other interests beside their primary job.  Some lucky folks can make money doing things they like.  I consider myself a lucky one because if I wasn’t a computer programmer, I would do it in my spare time.  Actually I’m still doing coding in my spare time working on some personal projects which probably will never make me a penny.  However, besides writing computer code I have other hobbies.  I enjoy photography, road cycling, tennis and some other activities at which I always will be just an amateur.

I always admire people who takes their hobbies to the next level and became professional at what they love to do.  One of mine co-workers tremendously enjoys nature photography.  He is making a next step to see if he is good enough to become a pro.  He created a site where people can admire and buy his pictures on canvas.  On his site visitors can view beautiful, unique photos from Yosemite National Park, Yellowstone National Park, Death Valley National Park and many other beautiful places.  All photos on his website can be printed on canvas and are available either in color o black and white.

What is missing on his website is a space where people could leave a comment for each picture.  As an artist you probably want to see feedback from others.  Well, may be he’ll add it later.  You cannot do everything at once.

Let’s wish him luck and go check out his site.

Posted in Miscellaneous | Tagged: | 1 Comment »

Gallio v.3.2.2 resolves problem with CS1685 warning.

Posted by Vadim on January 13, 2011

I was very pleased when I saw a Yann Trevin’s post about new version of Gallio and MbUnit.  What made me happy about this release is that the team resolved issue with CS1685 warning.  After I downloaded Gallio v3.2.2 and remove my temporary fix that deals with CS1685 warning, I was able to confirm the problem is gone.  If you’re using Gallio / MbUnit, go to Downloads page and get the msi for your system.

Posted in MbUnit | Tagged: | Leave a Comment »

Detect when another site is unavailable. (404 detection)

Posted by Vadim on December 25, 2010

A long time ago we used to store our code on punched cards.  Since that time, our development tools improved a lot.  Thanks to more powerful languages and tools, it became easier for us to write code, and as a result, our applications became more and more complex.  In 1986 Fred Brooks predicted exactly that in his article “No Silver Bullet”.  Today, any modern application uses third party libraries or components.  Our web apps need to integrate with other sites to use their services. But what do you do if the third party site is down?

At first, the problem doesn’t seem that difficult, but then I realized that it’s impossible to detect with JavaScript when another site is unavailable. (Please let me know if I’m wrong.)  If it would be possible to do this with client scripting, it would be huge security hole.  Since the release of Netscape Navigator 2.0 in 1996, browsers prevent access to most methods and properties across pages on different sites.  Today, this policy is known as Same Origin Policy.

The solution I came up with is to ping the third party host on the server side before JavaScript is loaded.  If I get a good response, then I proceed with my JavaScript; otherwise, it’s redirected to my error page.

Here’s a sample how you can check if the host available with ASP.NET:

protected void Page_Load(object sender, EventArgs e)
    HttpWebResponse response;
        var request = 
        response = request.GetResponse() as HttpWebResponse;
    catch (WebException webException)
        Response.Redirect("ErrorPage.aspx?status=" + webException.Status);

If GetResponse() throws a WebException, it means that something went wrong with our request.  We can figure out what’s wrong by analyzing the WebException Status and Response properties.

kick it on

Posted in .Net, ASP.NET, C# | Tagged: | 1 Comment »

Setting up RavenDB as an IIS application by pictures.

Posted by Vadim on December 13, 2010

I’ve been living all my professional live in relational database world.  Lately NoSQL is making a lot of buzz that I couldn’t ignore it any more and decided to take a closer look.   The first question I had is which NoSQL database should I play with?  I narrowed my option to three: CouchDB, MongoDB, and RavenDB.  Being a .NET developer RavenDB looked like a logical choice. It’s .NET friendly and has a LINQ provider.

Next step for me was to download and install RavenDB and that what this post is about.

1. Download RavenDB. 

You can go to RavenDb’s download page or just click here to grab the latest build.

2. Unzip the file.

Use your favorite file archiving utility to unzip the file you just downloaded.  In my case I used 7-Zip to unzip file.  By the way I decompressed the files intto C:\RavenDB folder.


3. Next let’s fire IIS Manager and create new application pool.

You need to open the IIS Manager.  The following instruction are for IIS 7.  If you are using different version than you’re on your own. 

In Connections panel select Application Pools.

Then in Actions panel click on the “Add Application Pool…” link.  If you clicked it correctly, you should see the “Add Application Pool” dialog.  In case you don’t see this dialog than you need to read How to Use a Computer Mouse.

You need to name the application pool and you must choose .NET Framework v.4.0 in the “.NET Framework version” drop down box.  Leave everything else the same and press the OK button.



4.  Create a new Raven Web Site.

In IIS Manager right lick on the Sites folder and select “Add Web Site…” from the context menu.


You should see the “Add Web Site” dialog. 

For Site name type “RavenWeb” or you can pick some other name.

Select RavenApplicationPool for Application pool.  You do that by click on Select button.

Make sure that Physical path is points to “Web” folder in extracted folder.  In my case it’s going to be “C:\RavenDB\Web”.

Leave everything the same except the Port.  I picked 8080 for my port you can type the same number or select different.

Your values should look similar to mine but don’t click the OK button yet.


Click the “Test Settings…” button and you probably will see that authorization failed.


We must provide the user for our site that has write access to the physical database location.  Close the “Test Connection” dialog and click on “Connect as…” button in the “Add Web Site”.

“Connect As” dialog should appear on your screen.  Select “Specific user” option and click on the “Set…” button to to enter credentials in “Set Credentials” dialog.


If you entered. proper credentials, than by pressing on the “Test Settings…” button you should see that Authentication and Authorization are valid and we’re ready for next step.


WebSiteCreatedPress the “Close” button for “Test Connection” and then “OK” button for “Add Web Site” dialog.  You should see that the new Web Site was created.


5. Done.

We successfully installed and set up Raven DB to run in IIS.  Just navigate to http://localhost:8080 url or in IIS Manager make sure the new web site is selected and click on “Browse *:8080 (http)” link in Action section.  Rave DB page should be opened in your favorite browser.


Posted in Database | Tagged: | 2 Comments »

NuGet Upgrade on 12/10/10

Posted by Vadim on December 10, 2010

There’s a new upgrade for NuGet.  To update it just go to Tools –> Extension Manager in Visual Studio to lunch Extension Manager dialog. In Updates you’ll see an update for NuGet Package Manager.  It will ask you to restart your VS.

Posted in NuGet | Tagged: | 1 Comment »

iPad and Russian Keyboard

Posted by Vadim on December 9, 2010

Last week I got a present, a new shiny iPad.  After downloading and playing with few games, I decided it’s time for me to install a Russian keyboard even I don’t type much in Russian.  Like any Apple’s Mac Book, iPad has an option for Russian Phonetic keyboard.  If you don’t know what’s Russian Phonetic Keyboard, read my previous post on the subject.

In order to set up Russian Phonetic Keyboard, I start Settings application.  Then go to General | Keyboard | International Keyboards | Add New Keyboard….  I add Russian – Phonetic.


When I try to use the keyboard that I just added, I’m unpleasantly surprised.  It’s Russian standard keyboard but I explicitly was adding Phonetic one.


If I’m doing something wrong, can someone just slap me and let me know what an idiot I am and what I need to do install it correctly.  Also Apple made a grammatical error when they named the keyboard “Русская” instead of “Русский”.  Like in many other languages, in Russian objects have sex and “keyboard” is male in Russian.  By saying “Русская” it makes the keyboard a female and that sounds way wrong.  You expect this error from people why just started learning Russian.

UPDATE: Thanks to Sergey who reminded me about Russian translation for word “keyboard” which is “клавиатура”.  Клавиатура has female sex and this case “Русская” would be appropriate.  I’m so accustomed to use word “keyboard” even when I speak Russian.


Posted in iPad | Tagged: | 17 Comments »

Baby steps to create a NuGet Package.

Posted by Vadim on December 8, 2010

You probably heard about NuGet (formally known as NuPack), an open source package management system for the .NET from Microsoft.  Jeff Kwak, who works with me at Ultimate Software, recently wrote a nice post about how to create a NuGet Pakage.  Jeff’s article inspired me to create a package for my own open source project, SystemWrapper.   In this post I want to describe step by step how I did it.

Step 1: Create a NuSpec file.

NuSpec file is an xml file that contains the metadata for a package.  I’ll try to describe every field of NuSpec format with an example.

In this first step we’ll going to use the minimum set of fields.

id The identifier for the package.  It used as a partial name of the package.
version The package version.  Also used as a partial name of the package.
authors Author name.  Use a comma to separate multiple authors.
description You know what it is.
    <authors>Vadim Kreynin</authors>
      SystemWrapper is a library that wraps .NET classes for system resources like System.IO.FileInfo, System.Reflection.Assembly, and many other classes so you can easily mock them.

I named the xml SystemWrapper.nuspec.  I believe that it is a good logical name for NuSpec file.

One of the way to add a package is to use Add Library Package Reference dialog.  I’ll talk more about adding a package and Add Library Package Reference dialog later in this article but for now I want to show where NuSpec fields a re used.  Bellow is the image that describes where above NuSpec metadata elements used in the dialog.


Step 2: Build the package.

First you need to download NuGet.exe command line tool.

Now when we have NuGet.exe, we are ready to create our SystemWrapper NuGet package.  Fire up your Command Console and type the following line.
NuGet.exe pack SystemWrapper.nuspec

If everything runs at it supposed to, you should see systemwrapper. file in your Windows Explorer.

As you can see the name of the file is [id].[version].nupkg.  id and version from our NuSpec file.

Step 3: Publish the package.

To release System Wrapper to the world we need to contribute to NuGet packages on CodePlex.  In this example I’m going to show how to host System Wrapper NuGet package locally.  You can follow this technic to host packages for your company.  I learned how to host NuGet packages locally from a great Phil Haack’s post.

First, I create a directory for my packages.  It has an original name ‘packages’.


Next, we need to add another package source.  Out of the box you should have only one package source which named ‘NuGet official package source’.  We are going to create another package source and I’m going to call mine ‘Vadim’s package source’.  I think that this is going to cement my legacy.

In order to add a package source in Visual Studio we need to go to Tools –> Options.  Inside the option dialog box select Package Manager and enter values for Name and Source and press Add button and then OK to close the Options dialog.


You also can get to this dialog by pressing Settings button in Add Library Package Reference dialog, which is the first image of this post.  The button located in the lower left corner of the dialog.

Step 4: Find the package in Visual Studio.

There are two ways we can browse for our package.  One way is using Package Manager Console which is Powershell based. Another one is Add Library Package Reference dialog that I mentioned earlier.

First let me describe how we can browse using Package Manager Console.  We can lunch the console by selecting View –> Other Windows –> Console from the menu or simply press Ctrl+W, Ctrl+Z.Navigation2PackageManagerConsole

SelectPackageSourceFromPMCAfter console is loaded, we need to select our package source.  Just pick it from the Package source drop down box.  In my case it’s “Vadim’s package source”.  Next, type List-Package at PM> prompt.  See the result bellow.


I don’t know why I cannot see my description in Package Manager Console.  It shows fine in Add Library Package Reference dialog.  Let me know if you have an answer for me.

AddRefOptionsNow let me show how you can browse for the package using Add Library Package Reference dialog.  The way I do it is just right click on References in the project to see my  add options.  Select “Add Library Package Reference…” to display the dialog.  The dialog has three panels.  The first panel has three sections: Installed packages, Online, and Updates.   “Installed packages” shows the package that installed for specific project.  The second one “Online” shows packages that are available.  We want to chose this option.  Select your package source.  The third option is updates.

The second panel shows the list of packages.  The third panel shows details of the selected package.


It doesn’t make scenes for us to install the package at the moment because we only describe the package but doesn’t add any libraries to the project.

Step 5: Add SystemWrapper.dll to the package.

Our goal is to add SystemWrapper to the project.  It means that we need to add assembly reference to SystemWrapper.dll. It’s very easy to do, we just need to add files element to our NuSpec file. files element must be on the same level as metadata element.

Here’s a fragment that we add to NuSpec metadata:

   <file src="SystemWrapper\bin\Release\*.dll" target="lib" />

file element has two attributes src and target.  I just want to let you know that src is a path that relative to the nuspec file.  Our target attribute has a value ‘lib’, it means that dlls under SystemWrapper\bin\Release will become assembly references on install.  In our case it’s going to be only one dll.  You can get more details on your own by reading the NuSpec File Element Specification.

Here’s the complete NuSpec file:


    <authors>Vadim Kreynin</authors>
      SystemWrapper is a library that wraps .NET classes for system resources like System.IO.FileInfo, System.Reflection.Assembly, and many other classes so you can easily mock them.
    <file src="SystemWrapper\bin\Release\*.dll" target="lib" />

After we updated NuSpec file, we need to repeat step 2 (Build the package).

Step 6: Install the package.

Once again you can install either from Package Manager Console or from Add Library Package Reference dialog.  It’s strait forward to install it from Add Library Package Reference dialog, you just press the Install button.  After the installation, I have SystemWrapper in my Installed packages.  I can easily uninstall it by pressing the Uninstall button.  How easy is that?  Also if I look in Online section, I can see that instead of an Install button there’s a green check mark for my package.

In Package Manager Console I just type Install-Package SystemWrapper at PM> prompt.

PM> Install-Package SystemWrapper
Successfully installed 'SystemWrapper'
Successfully added 'SystemWrapper' to PackageObjectLibrary
You can uninstall the package by typing Uninstall-Package SystemWrapper.
For more details check out Package Manager Console Commands.

Step 7: Display more information about the package.

When developers browse for packages, they probably would like to more information about a package than just a description.  Adding projectUrl element to NuSpec file will point users to the resource with more details.
Here’s an example for System Wrapper:

You might also want to add a link to your license terms.  Once again you need to add another field to the NuSpec file.  This time we need to add licenseUrl.  If you don’t want a developer to install your package unless she accepts the license terms, than you need to add requireLicenseAcceptance element. The xml might looks like this:


When a user tries to install the package, she will see a dialog similar to the one bellow:


You also can specify your own icon instead of default blue NuGet icon that appears in Add Package Reference dialog .  The element for an icon is iconUrl.  The icon should be a 32×32 pixel PNG file with a transparent background.


The complete NuSpec file might look something like that:

    <title>System Wrapper</title>
    <authors>Vadim Kreynin</authors>
      SystemWrapper is a library that wraps .NET classes for system resources like System.IO.FileInfo, System.Reflection.Assembly, and many other classes so you can easily mock them.
    <file src="SystemWrapper\bin\Release\*.dll" target="lib" />




I know that I didn’t talk about dependencies or modifying config files at all.  I just try to give a simple example to follow.  I hope that it can help you to get started with NuGet.  Here’s some references I would recommend for you to read:

Posted in NuGet | Tagged: | 6 Comments »