Thursday, March 23, 2017

C# Things I've Been Learning Lately

How to iterate over Enum types:
foreach (var myType in 

How to create a case-insensitive dictionary with a "string" key and a "MyType" value.
private static readonly Dictionary< string, MyType> _dictionary = 
  new Dictionary<string, MyType>(StringComparer.InvariantCultureIgnoreCase);
The Dictionary class takes an optional parameter of IEqualityComparer<tkey>. By passing in "StringComparer.InvariantCultureIgnoreCase" you create a dictionary that is case-insensitive for the string keys.
How to capitalize the first letter of each word in a string? String functions "ToUpper()" and "ToLower()" have a cousin, "ToTitleCase()" that capitalizes the first letter in each word
System.Globalization.TextInfo textInfo = 
        new System.Globalization.CultureInfo("en-US", false).TextInfo;
title = textInfo.ToTitleCase("we hold tHese tRuths tO be Self evident"); 
//We Hold These Truths To Be Self Evident

How to read data from an embedded file? First add the file to your project and set its Property "Build Action" to "Embedded Resource".
var assembly = Assembly.GetExecutingAssembly();
var name = assembly.GetName().Name;
var resourceName = $"{name}.MyTextFile.txt";

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
    using (StreamReader reader = new StreamReader(stream))
        while (!reader.EndOfStream)
            string line = reader.ReadLine();

Tuesday, March 21, 2017

Performance Profiler in Visual Studio Not Showing Function Names

In Visual Studio 2015 while doing performance profiling, I ran into the problem of memory addresses of functions being shown instead of their names.

During operations I got these errors:

VSP2340: Environment variables were not properly set during profiling run and managed symbols may not resolve.  Please use vsperfclrenv before profiling.


Error        DA0002: It appears that the file was collected without properly setting the environment variables with VSPerfCLREnv.cmd. Symbols for managed binaries may not resolve.        C:\Git\\MarketPlace.Web\Report170320(2).vsp    0   

Both of these issues should not happen if you are using the profiler inside Visual Studio.

The root cause was not running Visual Studio in Admin mode from the start.
Here's an example scenario to get functions names in Visual Studio 2015.

Start Visual Studio in Admin mode:

 Select from the tool bar "Analyze" > "Performance Profiler..." and then "Start"

 Select the appropriate method.  I've been using "Instrumentation" lately.

 Select your projects and then "Next"

Check "Enable Tier Interaction Profiling"

Then, "Next", and finally, "Finish"

IE should launch, and run through your test scenario.
 Then select "Stop profiling".

The Summary report will appear:

Then, if you select the "Functions" report, your functions should appear with a friendly name.