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) +
((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() +
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)
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:
- I believe it’s easier to read, but readability is very subjective.
- 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.
Please let me know if you can think up other implementations for capitalizing letters.
Posted in C# | Tagged: C#, capitalize, ToTitleCase, uppercase | 3 Comments »
Posted by Vadim on October 20, 2008
If you use ReSharper, you notice that every time you create a global class variable without a modifier, ReSharper will add a private modifier for you.
Some people may be prefer this way. I personally don’t see what values it adds. My brain already knows that if there’s no modifier for a variable, than this variable is private. I don’t need any extra information for my brain to process and filter.
Luckily for me ReSharper has an option to turn this feature off.
- Lunch ReSharper – Options dialog box.
- On the left part of the dialog select C#/Formatting Style/Other
- On the right part of the dialog uncheck ‘Use explicit private modifier’.
Posted in ReSharper | Tagged: C#, ReSharper | 6 Comments »
Posted by Vadim on October 16, 2008
When we learn any programming language, one of the first thing we discover is the syntax how to comment our code. Some of you probably already have been using this shortcut key for awhile.
Select multiple lines of code and press Ctrl+K,Ctrl+C, and you hard work is going to be ignored. To reverse just press Ctrl+K,Ctrl+U.
These keystrokes will comment/uncomment your code only with line comment like // in C# or — in SQL. I’m not aware how to comment code with block comment (/**/) without using a plug-in for VS.
One more thing. If you need to comment/uncomment a single line, you don’t need to select the whole line. Just move the cursor to any position on the line you want to comment, and let you fingers press the magic combination.
If you like me (there’s nothing wrong to be different from me) and use ReSharper, than you would like to use Ctrl+Alt+/ with line comment and Ctrl+Shift+/ with block comment.
Posted in .Net, Coding, ReSharper, Tips And Tricks, Visual Studio | Tagged: C#, ReSharper, Tips, Visual Studio | 5 Comments »
Posted by Vadim on June 16, 2008
Today I had to work with some legacy code. It was originally written in Delphi and then converted line by line to C#. Before changing anything in the code I decided to create unit tests and make sure that I have 100% coverage.
One of the thing I had to do in my unit test is to mock DataTable. Here are the steps I made during mocking.
- Create needed columns in a DataTable.
- Create a new DataRow.
- Assign values to the row.
- Finally add the row to the DataTable.
Repeat steps 2- 4 for each row you want to add to your DataTable.
Here’s the example:
2: public void HolidayTest()
4: MockRepository mocks = new MockRepository();
5: ICompanyDAL dalMock = mocks.CreateMock<ICompanyDAL>();
6: // Create DataTable
7: DataTable fakeHolidays = new DataTable();
8: // 1. Add Columns
9: fakeHolidays.Columns.Add("Holiday", typeof (DateTime));
10: fakeHolidays.Columns.Add("Name", typeof(string));
11: // 2. Create new DataRow
12: DataRow dayRow = fakeHolidays.NewRow();
13: // 3. Assign values to the row
14: dayRow["Holiday"] = DateTime.Parse("07/04/2008");
15: dayRow["Name"] = "Independence Day";
16: // 4. Add the row to the DataTable
18: _pgDate.CompanyDal = dalMock;
19: using (mocks.Record())
23: using (mocks.Playback())
25: DataTable holidays = _pgDate.Holidays;
26: Assert.GreaterThan(holidays.Rows.Count, 0);
I used Rhino.Mocks as my mocking framework in the example above.
Posted in .Net, C#, Coding, MbUnit, Rhino.Mocks, TDD | Tagged: C#, DataTable, Rhino.Mocks | 2 Comments »