Wrapping Code Block and Statements in Visual Studio

In this tips I am going to describe how you can automatically wrap up code blocks or code statement in Visual Studio. We can use Document formatting to format the whole contents, but that doesn’t wrap single line code block like Properties or not even single line multiple declaration statement.  Let’s consider an example, you are creating a Properties using code snippet and by default it will came up like
How to Wrapping Code Block and Statements in Visual Studio

But, you want you code to be look like as below

How to Wrapping Code Block and Statements in Visual Studio
Well, if you don’t know the trick, you have to do it manually. But VS provides you to enable this features automatically.

Goto Tools > Options > Text Editor > C# > Formatting > Wrapping. As shown in below image, by default “Leave block on Single line” is Checked. You can also see the example of “Age” Properties.
How to Wrapping Code Block and Statements in Visual Studio

Now once you unchecked the “Leave block on Single line” checkbox, you can see the change reflected on example itself.

How to Wrapping Code Block and Statements in Visual Studio

Similar, you can use the second checkbox option for “Same line Multiple Statement declaration”. As for example you have below set of code
How to Wrapping Code Block and Statements in Visual Studio
this is the default behavior of VS Code Editor. Now, if you want put all the statement in different line, instead of doing it manually you can use the “Leave Statements and member declarations on the same line” check box.

This will automatically changed the line as
How to Wrapping Code Block and Statements in Visual Studio

Advertisements

String Formating in c# 6

format strings

Formatting the string values are very common during development. We generally use String.Format() method, that replaces each format item in a specified string with the text equivalent values from the objects. While this is a common, sometimes this is confusing and produce error. Because, we need to specific the place holder for each item and then map them with the objects values. We also need to ensure that we are referring the correct object value for individual place holder. String Interpolation in C# 6.0 makes it much easier and clean. Using the new String Interpolation, we can directly refer the values rather specifying the place holders.

Here is a small example of using String interpolation.

string Value1 = "Value 1";
string Value2 = "Value 2";

// With String.Format
var stringValue1 = string.Format("{0}- {1}", Value1, Value2);

// With String Interpolation
var stringValue2 = "\{Value1}-\{Value2}";

In the above code snippet both stringValue1 and stringValue2 return the same values, but String Interpolation makes it look more easier and less error prone. Just think about when you are formatting with multiple properties values with some text just for display, you know where to place which values, rather using some placeholders.

Learn ASP.NET MVC 5 in 2 days

Questpond has launched a full step by step course on ASP.NET MVC. This course from Shivprasad Koirala will help you to learn MVC (Model view controller) step by step in 2 days ( i.e in 16 hours) right from basics to a professional level. This is a Sponsored post and in this post, the first video will show you how to display a simple Hello world in ASP.NET MVC. Also, check out the complete syllabus for details. Shivprasad Koirala has published a video course on Questpond, which will step-by-step guide you to learn MVC 5 in just 2 days
(yes, you heard the word correct… just 2 days i.e. 16 hours only). Below is the complete syllabus of the course “Learn MVC 5 in 2 days”:Lab 01 : – Simple Hello world (20 minutes) Lab 02 : – Explain MVC Routing (10 minutes) Lab 03 : – Explain ViewData, ViewBag, TempData & Session Variables (20 minutes) Lab 04 : – Explain Model and Strongly typed views (20 minutes) Lab 05 : – Explain Model Binders (10 minutes) Lab 06 : – Why MVC and MVC vs Webforms? (30 minutes) Lab 07 : – Explain TempData, Peek and…(Read more)