nameof Operator in C#

C# 6.0 introduced a new operator named, nameof  that accepts the name of code element and returns a string literal of the same element.  The nameof operator can take as a parameter like class name and its all members like method, variables, properties and return the string literal.  This avoids having hardcoded strings to be specified in our code as well as avoid explicitly use of reflection to get the names.

Below is the simple code snippets that show the uses of nameof operator.

class Program
{
    static void Main(string[] args)
    {
        WriteLine(nameof(Student));
        WriteLine(nameof(Student.Roll));
        WriteLine(nameof(Student.Name));
        WriteLine(nameof(Student.Address));
    }
}

class Student
{
    public int Roll { get; set; }
    public string Name { get; set; }
    public Address Address { get; set; }
}

The output would be like below :image_thumb

nameof Operator in C#

Null Propagation C#

Null propagation enable developers to check the null value with in an object reference chain. The null –  propagation operator ( ?.) , returns null if anything in the object reference chain in null. This avoid checking null for each and every nested objects if they are all in referenced and having null values.

Consider we have class called Student, and which having another property Address of type Address class. Now we can have the following code snippet to print the HomeAddress.

if (student != null && student.Address != null)
{
    WriteLine(student.Address.HomeAddress);
}
else
{
    WriteLine("No Home Address");
}

As we can see, to avoid  the null-reference exception, we have checked the null for student, and student.Address as they can have null reference as object.

Now, the above code block can be re-written using null – propagation( ?.) operator as follows:

WriteLine(student?.Address?.HomeAddress ?? "No Home Address");

Isn’t it really nice. Instead of check each and individual objects, using ?. we can check entire chain of reference together and whenever there is a null values, it return null.

Null Propagation C#

How to Place your reusable code into Toolbox in Visual Studio?

This is very important and small tips. Sometimes we have a small piece of code which can be used in different applications. What we do? We can create a common library and use that library in the different application. But there will be cases where we can not use this approach due to restrictions on naming conversion or namespace naming conversions or due to some other reason. So what we should do now.

Visual Studio having a great feature, that you can place your code snippet inside the toolbox. For that you need to just select the code snippet and drag the selected snippet on to the general Tab.

codesnippet

You can rename the selected snippet, by just right clicking on snippet and click on Rename.

renamecodesnippet

Now, you can use it anywhere in your application or in any VS Application by just drag and drop of the selected snippet.

How to Place your reusable code into Toolbox in Visual Studio?

Being More Productive With Code Snippets

Code snippets have been around for a long time and is one of the easiest ways for the developer productivity.  We do use many of them frequently as they are very commonly in used, however there are a few which are really useful but many of us does not know about them.

For an instance,  a very useful time saver  code snippet is  “prop”  which is use for “Automatically implemented property” . In this case, if we type prop and then press TAB key twice, an automatic property will be added with default type of int . ( Refer the image below)

prop_codesnippet_automatic_property

What if, if you want a property with declarative backing field ( Generally we defined them using a private variable and public exposed property  with same type)?  In this case rather typing the complete stuff, you can use propfullwhich gives you a private backing variable and then a public GET and SET with the link of just declared private variable.

propfull_codesnippet

You can change the type and variables name by just moving around the code block  using Tab Key press.

This will save a good amount of time compared to manual process.

Going forward, you can explore this kind of hidden snippet of  Visual Studio  just by navigating to “Code Snippets Manager” from “Tools –> Code Snippets Manager”. The following dialog window will help you to navigate through all the available snippet and the uses.

codesnippetmanager_thumb

Being More Productive With Code Snippets

Ways to get more productive in Visual Studio

1. Find active document in Solution Explorer

Using menu options like “Go to definition” is great. But sometimes you’re taken to a document and you have no idea where the document belongs, especially if you have plenty of projects. By clicking on the Sync with Active Document button (see picture below), Solution Explorer will navigate to the location where the current document exists and mark it.

capture

Continue reading “Ways to get more productive in Visual Studio”

Ways to get more productive in Visual Studio

What’s New in C# 7.0

C# 7.0 adds a number of new features and brings a focus on data consumption, code simplification and performance. Perhaps the biggest features are tuples, which make it easy to have multiple results, and pattern matching which simplifies code that is conditional on the shape of data. But there are many other features big and small. They all combine to make your more efficient and clear and make developers more productive.

Continue reading “What’s New in C# 7.0”

What’s New in C# 7.0

CREATE DATABASE (Azure SQL Database)

Creates a new database. You must be connected to the master database to create a new database.

CREATE DATABASE database_name  
    AS COPY OF [source_server_name.] source_database_name  
    [ SERVICE_OBJECTIVE =   
          {  'basic' | 'S0' | 'S1' | 'S2' | 'S3'   
            | 'P1' | 'P2' | 'P3' | 'P4'| 'P6' | 'P11'   
            | { ELASTIC_POOL(name = <elastic_pool_name>) } }   
    ]  
[;]

Copying a database using the CREATE DATABASE statement is an asynchronous operation. Therefore, a connection to the SQL Database server is not needed for the full duration of the copy process. The CREATE DATABASE statement will return control to the user after the entry in sys.databases is created but before the database copy operation is complete. In other words, the CREATE DATABASE statement returns successfully when the database copy is still in progress.

Continue reading “CREATE DATABASE (Azure SQL Database)”

CREATE DATABASE (Azure SQL Database)