Tag: .net

March 2013 Nicholls Presentation

This post will contain the information from my presentation at Nicholls State University on March 26, 2013.

Code: https://github.com/knight0323/presentations/tree/master/nsu-2013-03
Slides: https://docs.com/knight0323/2320/nicholls-dev-presentation-march-2013

Presentation Information

The general Visual Studio Tips and Tricks information can be found in my video linked below. I will be creating a Debugging Tips and Tricks video soon.
https://jeremyknight.wordpress.com/2013/03/18/visual-studio-tips-and-tricks/

Additional Reading Resources

Here is a list of resources for anyone that would like to do additional reading and/or research:

ASP.NET

LINQ

Entity Framework

ASP.NET AJAX Control Toolkit

Visual Studio Tips and Tricks

Visual Studio Tips and Tricks was a topic in our local user group a while back. I decided why not do a video instead of trying to publish all of the tips in a PowerPoint or some write up. Everything shown here can be done in at least Visual Studio 2010 and Visual Studio 2012.

.NET String Concatenation

String concatenation is a tool on every developer’s tool belt but in .NET there are multiple ways to accomplish it. There are also a lot of conflicting articles, posts, etc. on the subject. When should you use StringBuilder? When should you use string formatting? This article will hopefully shine some light on when to use each method.

Which Methods Were Tested?

How Were They Tested?

A console application was prepared to test operation scenarios. The scenarios were timed using the System.Diagnostics.Stopwatch class. The full Visual Studio solution can be found in my CodePlex project.

What Were the Results?

Scenario 1: Loop Concatenation

The Loop Concatenation scenario was built to test string concatenation within a loop. This data set had the most straight-forward results. The StringBuilder class gives the best performance when using concatenation in this scenaraio.

Loop Concatenation Results (in milliseconds)

Code sample using StringBuilder in this scenario:

var builder = new StringBuilder();

for (int i = 0; i < this.iterations; i++)
{
    builder.Append("x");
}

var testString = builder.ToString();

Scenario 2: Full Name Concatenation

The Full Name Concatenation scenario was built to test simple string concatenation in which few concatenations occur. It concatenates first name, a space, and last name. This is commonly used to build display names for a UI. These results point to the String.Concat method as the most efficient way to concatenate small numbers of strings.

Full Name Concatenation Results (in ticks)

Code sample using String.Concat in this scenario:

string first = "John";
string last = "Deaux";
string fullName = string.Concat(first, " ", last);

Scenario 3: Long Text Concatenation

The Long Text Concatenation scenario was built to test long concatenations in which many concatenations occur. It simulates building the body of an email message. These results also point to the String.Concat method as the most efficient. Comparing the prior scenario with this scenario you can begin to see pattern. As you add concatenations, the efficiency of String.Format and StringBuilder (when out of a looping scenario) declines.

Long Text Concatenation Results (in ticks)

Code sample using String.Concat in this scenario:

string newLine = System.Environment.NewLine;
string name = "John Deaux";
string email = "john.deaux@123.me";
string subject = "The Subject of the Message";
string product = "ABC";
string feature = "XYZ";
string body = "The comment(s) made about product/feature.";

string[] values = new[]
    {
        "Name: ", name, newLine,
        "Email: ", email, newLine,
        "Subject: ", subject, newLine,
        "Product: ", product, newLine,
        "Feature: ", feature, newLine,
        "Message: ", newLine, body
    };

var emailBody = string.Concat(values);

Scenario 4: Date Concatenation

The Date Concatenation scenario was built to test the formatting of dates. It formats a date into the sortable format of 2011-12-31T15:30:15. Of the people I’ve talked to about this little experiment, this one has surprised the most. Why? Because it is highly touted by articles, books, and even Microsoft as the way to format data and it’s extremely inefficient. The String.Format method is slower and less efficient than every other method tested, including the StringBuilder class, for formatting DateTime objects as strings.

Date Concatentation Results (in ticks)

Code sample using String.Concat in this scenario:

DateTime date = DateTime.Now;

var values = new[]
    {
        date.Year.ToString("0000"),
        "-",
        date.Month.ToString("00"),
        "-",
        date.Day.ToString("00"),
        "T",
        date.Hour.ToString("00"),
        ":",
        date.Minute.ToString("00"),
        ":",
        date.Second.ToString("00")
    };

var sortable = string.Concat(values);

Yes, you did read that correctly. The above code is a lot faster and more efficient than:

var sortable = string.Format("{0:u}", DateTime.Now);
// or
var sortable = string.Format("{0:yyyy-MM-ddTHH:mm:ss}", DateTime.Now);

My Visual Studio Theme

So I’ve finally published my Visual Studio theme on the Studio Styles website. It’s a customized version of the Ragnarok Grey theme created by Tomas Restrepro. I darkened the background then had to make a few other subtle changes for readability due to the darker background.

Ragnarok Dark as I’ve chosen to call it can be found here: http://studiostyl.es/schemes/ragnarok-dark-grey-customized

And remember to rate it if you like it!

Now a bonus for those of you that use ReSharper. The following line pasted into the bottom of the items section in the XML downloaded from the site before you import will make those To Do Comments and Not Implemented Exceptions stand out.

<Item Name="ReSharper Todo Item" Foreground="0x0000FFFF" Background="0x02000000" BoldFont="Yes"/>

2012 Baton Rouge SQL Saturday & Tech Day

The information for this year’s Baton Rouge SQL Saturday & Tech Day came online this past week. It’s scheduled for August 4, 2012.

Free SQL Server, SharePoint and .NET training!  Chance to win great prizes!  What more could an IT Professional ask for?  If this sounds good to you, then don’t miss your opportunity to attend SQL Saturday #150 and TECH Day, the largest FREE training event in Louisiana dedicated exclusively to SQL Server, .NET, Development, SharePoint and Business Intelligence.   This is a COMMUNITY driven event!

For more information visit the official website at: http://sqlsaturday.com/150/eventhome.aspx