Checking compatibility with .NET Portability Analyzer

This article is also available on the Microsoft TechNet Wiki.

With multiple project types now available in Visual Studio, it can sometimes get confusing which flavours of .NET work with which frameworks. This can now be compounded with .NET Core. Thus, creating code that is reusable means you need to take all factors into consideration so you can get maximum portability. Luckily, there is a tool that can help you port your .NET projects.

The Microsoft .NET Portability Analyzer is a free Visual Studio Add-in that offers a detailed report on the flexibility of your code. The report gives you an idea of how much work you need to do to make your application, library or project workable on other platforms, including .NET Core.

In this article, we will see how to install, set up and run the tool so we can see the detailed report it generates.

Read more of this post

Learn ASP.NET Core with these videos

ASP.NET CoreMicrosoft Channel 9 icon is a hot topic now amongst Microsoft developers. ASP.NET Core is a “lean and composable framework for building web and cloud applications”. The entire code base is open source and can be used to build cross-platform apps on both .NET Core and the full .NET Framework.

Microsoft knows developers want to learn this new way of building apps. They have always been excellent at delivering online learning tools and the same is true for ASP.NET Core. Microsoft Ignite was held in Atlanta in late September and they have published 749 of their session videos onto both the Microsoft Ignite On-Demand site and Channel 9.

Microsoft Ignite

For developers looking for the latest info on ASP.NET Core, check out these Ignite videos:

  • Explore web development with Microsoft ASP.NET Core 1.0 (Video | Slides)
  • Explore the new, cross-platform .NET Core 1.0 (Video | Slides)
  • Dive deep into ASP.NET Core 1.0 (Video | Slides)

For those looking for an updated overview of the Microsoft developer ecosystem, there is this video also from Ignite by Scott Hanselman:

  • Review the Microsoft application platform for developers (Video)

Microsoft Virtual Academy

Finally, the Microsoft Virtual Academy has posted two new ASP.NET Core videos:

I hope these video tutorials help you learn what .NET Core and ASP.NET Core has to offer.

Create GUID Tool in Visual Studio

This article is also available on the Microsoft TechNet Wiki.

A globally unique identifier or GUID is an id that is exclusive throughout the globe. They are usually 128 bits long and are shown in hexadecimal groups separated by hyphens.

Within Visual Studio there exists a standalone tool (guidgen.exe) that allows developers to create GUIDs in a specific format. Creating GUIDs in code is easy enough but perhaps you need to generate one (for a constant or while unit testing) without running your code. This tool can help with that.

Creating GUIDs

  1. To find the tool, open Visual Studio and click Tools > Create GUID.GUID_01
  2. The Create GUID window will appear.
    GUID_02
  3. There are seven different GUID formats to choose from. As you move through the types, the Result window will display what the GUID value will look like.
  4. If you want a different GUID, click the New GUID button.
  5. Once you are happy with your result, click the Copy button to save it to the Clipboard. You can then paste it to your desired locale.
  6. Click Exit to close the dialog window.

Troubleshooting

If the tool does not appear under the Tools menu, click Tools > External Tools (see top image). It may be that the tool was not registered correctly within the IDE. Using the External Tools window, we can add a reference to guidgen.exe.

  1. Click the Add button. A new tool will be added to the Menu contents.
  2. Fill in the fields like so:
    • Enter a Title of Create &GUID. The ampersand will create a hotkey using the letter G.
    • For the Command field, browse to the Tools folder of your current Visual Studio install and find guidgen.exe. This location will typically be %Installation Path%\Microsoft Visual Studio {Version Number}\Common7\Tools\guidgen.exe. For Visual Studio 2015, the full path will look like this: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools. NOTE: You can also double click the exe here to run it. You will see the same Create GUID UI as above.
    • For the Initial directory, use the guidgen.exe root folder or %Installation Path%\Microsoft Visual Studio {Version Number}\Common7\Tools\
  3. When you are done, your entry should look something like this:GUID_03
  4. Click OK.
  5. Your tool should now appear in the Tools menu.

See Also

Using the Obsolete Attribute in C#

This article is also available on the Microsoft TechNet Wiki.

The Obsolete Attribute marks elements like classes, methods, properties, fields, delegates, and many others within your code as deprecated or obsolete.The attribute is read at compile time and it is used to generate a warning or an error to the developer.

This attribute can help you if you have ever wanted to make sure programmers use newer versions of your methods. It also makes it easier when you are transitioning from older methods to newer ones. Marking an item as obsolete warns users that program elements will be removed in future versions of the code base.

Obsolete Attribute

The attribute is found in the System namespace. The Obsolete attribute decorates a program element by putting the word “Obsolete” above it inside square brackets. Since it is an attribute, you can use either Obsolete or ObsoleteAttribute.

You can use it without arguments and in which case it generates a generic compile-time warning. It can also take one or two optional parameters: Message and IsError.

The Message parameter is a string value with the deprecation message. The message should state that the program element is obsolete and, if possible, point them to the new element to be used.

The IsError parameter is a Boolean value that tells the compiler whether to generate an error or not. An error is generated when IsError is true. If it is set to false, or is not included, then only a warning is generated.

Code Sample

The following code sample shows how to decorate methods with the Obsolete attribute:


using System;

class Program
    {
        // Mark Method1 obsolete without a message.
        [ObsoleteAttribute]
        public static string Method1()
        {
            return "You have called Method1.";
        }

        // Mark Method2 obsolete with a warning message.
        [ObsoleteAttribute("This method will soon be deprecated. Use MethodNew instead.")]
        public static string Method2()
        {
            return "You have called Method2.";
        }

        // Mark Method3 obsolete with an error message.
        [ObsoleteAttribute("This method has been deprecated. Use MethodNew instead.", true)]
        public static string Method3()
        {
            return "You have called Method3.";
        }

        public static string MethodNew()
        {
            return "You have called MethodNew.";
        }

        public static void Main()
        {
            Console.WriteLine(Method1());
            Console.WriteLine();
            Console.WriteLine(Method2());
            Console.WriteLine();
            Console.WriteLine(Method3());
        }
    }

The messages generated in the Error List appear as:

Error List

The two warnings are shown, as is the requested error.

References

Understanding the Visual Studio AssemblyInfo Class

This article is also available on the Microsoft TechNet Wiki.

This article won a silver medal in The Microsoft TechNet Guru Awards! (September 2015).

Overview

The AssemblyInfo Class holds application attributes about a Visual Studio project that are applied at the assembly level. These global assembly attributes can be information on the company, product, or even the languages it supports. The class is created automatically within the project and is housed in the Properties folder.

Setting the attributes

Setting attributes of the AssemblyInfo class can be accomplished two ways:

  • through the Assembly Information dialog box of your project’s Properties.
  • through the AssemblyInfo.cs file located within your Solution Explorer.

Assembly Information dialog box

To access the Assembly Information dialog box, double click the Properties file within your project’s solution on the Solution Explorer. This will raise the Properties Editor. Select the Application tab and then click the Assembly Information button to open the Assembly Information dialog box.

AIC01

Modify the fields within the dialog box and click OK to save your settings. All changes will be saved within the AssemblyInfo.cs file. For more information on the Assembly Information Dialog Box, see Assembly Information Dialog Box.

AIC02

AssemblyInfo.cs file

All of the information saved within the Assembly Information dialog box gets stored in the AssemblyInfo.cs file. This class file is maintained by Visual Studio and can be seen by expanding the Properties node in the Solution Explorer.

Open the file to see the project information located within the Assembly Information attributes. You can modify these attributes here for quicker editing instead of using the GUI editor.

AIC03

Assembly Information attributes

The Assembly Information attributes are designed to house specific information about your project. The following table shows each attribute’s function:

Attribute Function
[AssemblyTitle] Specifies a title for the assembly.
[AssemblyDescription] A description of the product or modules that comprise the assembly.
[AssemblyConfiguration] Specifies the build configuration, such as debug or release.
[AssemblyCompany] Holds company information.
[AssemblyProduct] Displays product information.
[AssemblyCopyright] Holds copyright information for product or assembly.
[AssemblyTrademark] Assembly or product trademark information.
[AssemblyCulture] Provides information on what languages the assembly supports.

Accessing the attributes through code

To access the values of the AssemblyInfo.cs file within your C# code you will need to employ the System.Reflection namespace. The following code sample shows how to extract this information for your project:

        public static void Main()
        {
            string company = ((AssemblyCompanyAttribute)Attribute.GetCustomAttribute(
                Assembly.GetExecutingAssembly(), typeof(AssemblyCompanyAttribute), false))
               .Company;
            Console.WriteLine(company);
 
            string copyright = ((AssemblyCopyrightAttribute)Attribute.GetCustomAttribute(
                Assembly.GetExecutingAssembly(), typeof(AssemblyCopyrightAttribute), false))
               .Copyright;
            Console.WriteLine(copyright);
 
            Console.ReadLine();
        }

AIC04

Conclusion

This article showed where assembly level metadata is stored for your projects. It also demonstrated how to modify this information, what the field attributes are used for, and then how to use them within your code. Since each project receives an AssemblyInfo.cs file automatically, you now know how to prepare this information for use within your projects.

References

Namespace Aliases in C#

This article is also available on the Microsoft TechNet Wiki.

The C# language allows developers to create aliases for lengthy fully qualified names or class names. This can come in handy if there is a long namespace or class name that you do not want to have to keep typing out each time. It is also a useful tool to resolve namespace clashes within your code when identical types are imported into your code base.

To accomplish this you can employ the using keyword in C#. The alias lives within the scope of the namespace it Is declared in. When the code compiles, a token is created for the fully qualified name and the alias is then in effect.

For example, the following code snippet will declare an alias:

using diag = System.Diagnostics.Tracing;

The image below displays how to fully declare and use the alias:

2015-09-11_20-50-45

References

C# Escape Characters

This article is also available on the Microsoft TechNet Wiki.

Like other C-based languages, C# makes use of Escape Sequences or Escape Characters when working with string literals. Escape characters are designed to ensure that special text in a string literal appears as it should. The most common usages are for single and double quotes, tabbed spaces, and new line characters. Every escape character begins with a backslash (\) and is followed by a value or token.

The most common escape characters are represented in the table below:

Escape Character Representation
\’ Single quotation mark
\” Double quotation mark
\\ Backslash (useful for file and network paths definitions)
\? Literal question mark
\a System alert (beep)
\b Backspace
\f Formfeed (next page)
\n New line
\r Carriage return (beginning of a line)
\t Horizontal tab
\v Vertical tab

Note: If a character that does not appear in the table above is preceded with a backslash, the character value is returned. For example, if the compiler sees \x it will be treated as x.