Category Archives: .Net

C# 6.0 – Elvis operator and other improvements

Title

Microsoft has released yet another version of .Net framework (4.6) and C# (6.0). In this post I am highlighting few improvements released as part of C# 6.0 which will largely benefit day to day development activities of a C# developer.

Elvis operator or formally called null conditional operator (?.) makes the null check simple. In a highly hierarchical object structure this operator reduces the length of code required to check for null and proceed further.

public class Sale
{
public Order orderItem { get; set; } = new Order();
}
public class Order
{
public int orderId { get; } = 101;
}

To read the value of orderId from main() class, null checks are required on objects of two of its hierarchical structure.

Sale sale = new Sale();

if(sale != null && sale.orderItem !=null && sale.orderItem.orderId > 0)
Console.WriteLine(string.Format("In older C#, value is {0}", sale.orderItem.orderId));

if (sale?.orderItem?.orderId >0)
Console.WriteLine($"In 6.0 C#, value is {sale.orderItem.orderId}");

Elvis operator makes this simple by evaluating the above expression left to right. The expression sale?.orderItem?.orderId >0 evaluates to orderItem if the left operand sale is non-null; otherwise, it evaluates to null.

In the above class definition, you will observe the way I have initialized the properties. I have directly initialized orderItem next to the property definition and it’s called auto-property initializer. Also you will notice orderItem has only get and no setter, this feature is called get-only auto property.

Also in the above code block you will notice, first I used string.Format to format the string and latter a special and simplified template to do the same. This is again a new feature in C# 6.0 called Interpolated Strings. An interpolated string expression creates a string by replacing the contained expressions with the ToString representations of the expressions’ results.

Advertisements

Bdotnet – Visual Studio 2013 Community Launch

VisualStudio2013b

This Saturday (14-December-2013) I was privileged to attend my 1st Bdotnet event in Microsoft office Bangalore. I had started exploring VS 2013 from the RT build and every time it gave me new stuffs to rejoice. So was I exited to hear more from experts on various new features, tips and tricks.

First session was by Tejasvi Kumar who made official community launch of Visual Studio 2013. He introduced few exiting features of VS 2013, including Visual Studio Online and CodeLenses. Just before this session, filler by Lohith was awesome. I would love to be part of such discussion. Many a times we try to discover new stuffs, advanced stuffs but start to forget basics. Lohith touched basics of web request response.

Later session was by Vic Parmar, who took us through new features introduced in VS 2013. One of the feature was Synchronized settings with which one can take his settings across devices. Those settings include, user preferences such as chosen color schema, text editor settings etc. I would expect much more from this sync settings such as synching of extensions and add-ons added. So that once you setup your environment, wherever you go and login to VS, sync will bring those settings to you and you will get homely feeling.

Next sessions were by Rajasekharan Vengalil and Karthikeyan Anbarasn on developing cloud based applications and Windows Azure applications. Since I had to rush for some other appointment, I could not attend rest of the sessions, but I am sure, I missed the session by Lohit on MONACO.

Overall the event was well organized. Thanks to the entire BDotnet team.

Visual Studio Online

vslaunch02

Wow!! This the word I exclaimed when I first saw Visual Studio Online. This is combination of collaboration, planning, version control tools and over cloud. Exciting??!!   Actually this is not something new, Microsoft has renamed their existing Team Foundation Service as Visual Studio One.

As soon as I installed Visual Studio 2013, it asked me to create http://___.visualstudio.com profile. That’s it!!! I am now ready with all above said combination of tools to start.

VSO1

You can choose among TFS and Git as your version control system and this gets integrated with your Visual Studio IDE. So as you create the project, you can start collaborating with your team members. Also there are various out of box agile/scrum templates to manage your epic, stories and iterations. In addition to these, VSO (Visual Studio Online) has quality related tools integrated. You can do code reviews, build, test plans, test case execution etc.

Advanced VSO even provide lighter version of Visual Studio to edit code, automated builds, load tests, etc. With Visual Studio Online, Microsoft is offering broad range of tools and services that support the different kinds of development code named “Monaco” specifically designed for building and maintaining Windows Azure Websites.

Visual Studio 2012 – Paste XML as Classes

images (1)

Here we have a new version of Microsoft Visual Studio, Visual Studio 2012.

This new version is coming out at the time of diversification of computing technology and platform. So this new version is loaded with new capabilities for Windows 8, the web, SharePoint, mobile, and cloud development.

This new version has brought a lot of new and interesting features. Out of which today I was more exited with feature “Paste XML as Classes”.

Paste XML as Classes

Say you have a well formed XML file and need to deserialize it to a fully qualified .Net type objects. Building this manually is a very dull and monotonous job. Visual Studio 2012 has a new feature “Paste XML as Classes”.

Below is one sample well-formed XML.

Copy the XML content and open a new class file. From the menu select select Edit | Paste Special | Paste XML as Classes as shown below.

Visual Studio will automatically generate the code as shown below.

SQL Server – Managing huge number of Server connections

My daily work involves connecting to several number of database servers and this involves me to remember server name or IP, port number, User Id and Passwords of those servers.  Number of servers I connect is more than 20 and due to this huge number I tend to forget the IP address, password etc. I can use the option of remember password, which will remember and list all the required server details with user id and password.

But the problem starts once you need to connect to a particular database server using more than one User Id. Once you use 2nd user id, SQL Server Management Studio fails to remember any of the passwords for that server.

So what is the solution?

SQL Server Management Studio has an option to manage server details, i.e. Registered Servers. By registering the server connection in the Registered Servers component of SQL Server Management Studio you can save the connection information for servers that are accessed frequently.
This option can be found under View menu.

A separate window will appear on the left hand side of the SSMS along with Object explorer. On right-clicking on Local Server Groups, option to register new server or to create new server group will appear. Creating Server group is optional but it will help in managing large number of server connections by logical grouping.

When Server Registration is opted, below window will appear. Opt for remember password option to remember the server connection.

In the above window you will find another tab which can be used to customize the connection further.

  1. Default database can be set here, so that every time you connect to a particular server from the registered server option, it will by default connect to the default database.
  2. Choose custom color for each server connections, so that one can easily differentiate between each connection.