C# 7.0 – Tuples Re-Engineered

Tuples have been around for quite sometime as a convenient way of returning more than one value from a method. Even though Tuples were quite easy to use but from code-readability perspective, there were certainly room for improvement. In C# 7.0, team Microsoft made lot of enhancements with introduction of Tuple Types and Tuple Literals. Let’s review those changes. A typical use of Tuple could look as follows.

static void Main(string[] args)
 {
    Tuple<int, string> info = GetInfo();
    Console.WriteLine($"{info.Item1} -> {info.Item2}");
 }

static Tuple<int, string> GetInfo()
 {
    return new Tuple<int, string>(1, "Joe");
 }

With the new syntax, the GetInfo method can be rewritten as follows. This is basically Tuple Literals where the data items are comma separate within the parenthesis. Also notice that the function return type is using same syntax.

static (int, string) GetInfo()
{
    return (1, "Joe");
}

On the caller side, you can still use old way of calling the method.

static void Main(string[] args)
{
    (int, string) info = GetInfo();
    Console.WriteLine($"{info.Item1} -> {info.Item2}");
}

Of course, Type inference could still be applied as well when calling the method.

static void Main(string[] args)
{
    var info = GetInfo();
    Console.WriteLine($"{info.Item1} -> {info.Item2}");
}

As shown in image below, the tool-tip is showing appropriate type has been inferred.

1

However, the  declaration of Tuple without any variable names doesn’t make code easily readable and understandable. Good news is there is a better way. The code snippet below shows a newer version of GetInfo method. In this case, we have used the names of the variables which makes it much easier to understand as to what data is returned from this method.

static (int id, string name) GetInfo()
{
   return (id:1, name:"Joe");
}

These variables names can also be used in the Caller side as shown below.

static void Main(string[] args)
 {
   var info = GetInfo();
   Console.WriteLine($"{info.id} -> {info.name}");
 }

The type inference also shows these variable names as shown below.

2

When consuming the Tuples, there is even better way by what’s called “deconstructing” a tuple.  Code snipping below shows that the value returned by GetInfo method does not have a variable name but its consumed in the Console.WriteLine method just the tuple variable names.

static void Main(string[] args)
{
    (int id, string name) = GetInfo();
    Console.WriteLine($"{id} -> {name}");
}

For the var junkies out there, the code snippet above can be used as follows also.

static void Main(string[] args)
{
   (var id, var name) = GetInfo();
   Console.WriteLine($"{id} -> {name}");
}

And to simplify it even further, you can do as follows just with one var.

static void Main(string[] args)
{
   var ( id, name) = GetInfo();
   Console.WriteLine($"{id} -> {name}");
}

Until next, happy debugging!!

Posted in .NET, CodeProject, Visual Studio 2017 | Leave a comment

Visual Studio 2017 – Simplified Object Initialization

Visual Studio 2017 introduced code refactoring to simplify object initialization. Let’s say we have following class called Bar.

1

And here is how this class is consumed.

2

Notice the Gray dots under the new keyword in this picture. This is an indication that there is some code refactoring available. In order to view it, just click the Ctrl+. and it will bring up following pop-up.

3

As you can see in this image, the suggested change is to initialize the BarId and BarName properties using Object Initializer. If you choose to apply this suggestion, the updated code will look as follows.

4

Until next, happy debugging.

 

Posted in Uncategorized | Leave a comment

Visual Studio 2017 – Add Braces

Visual Studio 2017 code refactoring improvements also made it easy to adapt to some best practices. One of those is adding braces around your code blocks. Let’s say you have code as follows. There are quite a few if -else conditions handled here but not wrapping this type of code in braces can generate lot of interesting discussion.

0

My goal is not to re-ignite that type of discussion here again because there is enough already available on internet. What I would like to point out here the new refactoring improvements that can make adding those best practices a little easier. If you look closely, you will notice those gray dots showing up under the if clauses as displayed in red rectangles in image below.

1

Those gray dots basically provide a hint that you can apply some code refactoring here using old friend Ctrl+. . Doing it will bring up following dialog for adding the braces. As you can see in blue rectangle in image below, you get the option to apply this in your Document, Project or entire Solution.

2

Let’s say I choose Document here. Doing this will bring up following dialog with preview and a chance to fine-tune where you exactly want to apply this.

3

At this point, if you hit Apply button, the changes will appear in your code as shown below.

4

Until next, happy debugging.

 

Posted in Visual Studio 2017 | Leave a comment

Visual Studio 2017 – Structure Visualizers

Structure Visualiser is one of the  popular features from Productivity Power Tools in 2013 and 2015 versions. Good news is that with Visual Studio 2017, the Structure Visualiser comes out of box without need to install anything.

In case you don’t know what is the Structure Visualiser, it is defined as follow here:

Structure Visualizer adds visual cues to syntactically signify blocks of code. The blocks allow you to quickly figure out the scope of a class, method and many other code scopes. Hovering over the block visual cues brings up a preview tooltip to help you visualize the scope you’re in.

To understand this, let’s inspect the picture below where you can see that the class Foo is visible on the screen. However, the class above Foo is not visible on the screen although this class methods GetBarId and SetBarId are visible.

In simplest terms, Structure Visualiser provides an easy way to view the name of class or methods partially visible without need to scroll anywhere else on the screen. On the left side, you can see vertical dotted lines that could be used to get that type of information. For example if you hover your mouse over the middle vertical line, it shows the namespace and class name as shown below.

Similarly if you hover mouse over the vertical dotted line, it shows the method name as shown below.

If you don’t want to see these lines, it can be turned on-off from “Show structure guide lines” available  Tools -> Options -> Text Editor -> General menu item as shown below.

Until next, happy debugging!

 

Posted in Visual Studio 2017 | Leave a comment

Visual Studio 2017 Code Refactoring – Convert Property To Method

Sometimes you start with creating a class property but later you realised that there is much more work in that property and it’s better to have it as a method. Visual Studio 2017 converting a property to a method quite trivial. Let’s say we have a class Bar with a property BarId as shown below.

1

Let’s say you want to convert it to a method. For that, you just click on the property name BarId and then bring up Refactoring helper by press “Ctrl + .”. This will pop-up the option for Replacing BarId with methods as shown below. The preview window shows that change that will happen due to this refactoring as well.

2

Once you preform the refactoring, the code will be changed to have a private field barId and a pair of setter/getter method for this field as shown below.

3

Until next, happy debugging.

Posted in Visual Studio 2017 | Leave a comment

Visual Studio 2017 Code Refactoring – Sync up Class and File names

Sometimes your C# file name and class name are different for whatever reason and you want to fix it. The usual way of “fixing” this issue is either by renaming the file name in solution explorer or changing the class name using Ctrl R + Ctrl R shortcut. None of these are two that bad of options but Visual Studio 2017 has introduced a better way of handle this scenario. Let’s consider following scenario where class name is Foos whereas the file name is Foo.cs.

RenamingFile1

Let’s say the class Foos is used as follows.

RenamingFile3

Within Visual Studio 2017, if you click the cursor on the class name and then press the Quick re-factoring shortcut (Ctrl + .), you will get the two options of either renaming the file or renaming the class as shown in figure below.

RenamingFile2

 

Let’s say I choose to rename the type of Foos to Foo from the menu item. With one click, it will not only rename the class itself but also wherever this class is referred as shown below.

RenamingFile4

 

RenamingFile5

Until next, happy debugging!!

Posted in Uncategorized | Leave a comment

Visual Studio 2017 Code Refactoring – Moving a class into its own file

Visual Studio 2017 has introduced new quick ways to refactor your code. One such refactoring is about moving a class in a new file. There are use cases when a developer may have created many classes in a single file. As shown in figure below, Let’s say I have two classes Foo and Bar in the same file called Foo.cs.

Capture

With Visual Studio 2017 refactoring improvements, its now much easier to move classes into a separate file of its own. In order to do that you just move cursor on the class name Bar within the Editor and right click. This will show the familiar menu options including “Quick Actions and Refactorings”.

Untitled

You can either choose this menu option or the shortcut Ctrl + . which will bring up the new refactoring option of “Move type into Bar.cs”.

Untitled2.png

Once you choose that option, the class Bar will be moved to a new file called Bar.cs as shown in image below.

Capture2

Until next, happy debugging!!!

 

Posted in Visual Studio 2017 | Leave a comment