Archive for the ‘.NET’ Category

C# 6.0 – nameof Operator

Posted: January 1, 2016 in .NET, C#
Tags: ,

In this post, we will discuss about the C# 6.0 nameof operator.

In the code snippet below we are trying to the details i.e Class & method names whenever a method is invoked. In real life applications we do similar kind of logging using any logging library.

image

The issue here is we have used string literals to refer to the class and method names. As the number of methods and corresponding log entries increases it becomes difficult to track these and also typos etc. may creep in creating more clutter.

In C# 6.0 the nameof operator gives us a provision to get the names of classes, methods,properties, variables etc. in the form of unqualified strings. The changed version of the code using nameof operator is shown below:

image

This gives as better manageability as the tools and IDE can have better track of these names now. Suppose I am renaming the class in that case Visual Studio track this down very easily as shown in the figure below

image

As obvious this is nothing but a very handy syntactic sugar coat. If we take a look into the IL code, we can easily see nothing heavy duty is going on inside. The compiler is simply replacing the nameof calls by the unqualified string literal name of the class and the method.

 

image

ASP.NET 4.5 has introduced model binding and strongly typed templates for ASP.NET server controls.This simplifies the way a plain .NET object can be bound as a datasource of a server control like a GridView.Let’s get started off with a simple example of how model binding works.

(more…)

Singleton is the most widely known Design Pattern. Singleton makes sure that one only one instance of a class is created and the same instance is used by other programs and classes.An implementation of a Singleton class is shown below:

(more…)

new Keyword in C#

Posted: July 29, 2012 in .NET, C#
Tags:

The “new” keyword in C# is one of the most commonly known and used keywords in C#.It is used as an operator for instantiating objects of a class as shown below.

(more…)

In the last post we have discussed at length about different mechanisms of parameter passing and obtaining results from a workflow.In this post we will discuss about another extremely important aspect of workflow and host communication i.e. Bookmarks. Bookmarks as the name suggests is a like Bookmark for a book. When created at a particular point in the execution of the workflow , the execution is suspended and the workflow waits to be resumed.When a bookmark is resumed it starts executing exactly from the same point.

(more…)

Workflow host is an application responsible for executing the workflow and managing different workflow lifecycle events like start, completion, idle, unloaded etc.Now the workflow host should be capable of exchanging information with the workflow. This information exchange can be broadly categorized into:

a) The parameters/arguments needed to start the workflow and to get the results back after workflow has executed.

b) The intermediate data needs at different stages of a long running workflow (e.g. when the workflow is waiting for a specific input like Manager’s Approval).

In this post we will concentrate on the different mechanisms of parameter passing and getting results back. In the subsequent posts we will move into more complex scenarios related to long running workflows.

WF 4.0 provides two classes for hosting and managing the workflows System.Activities.WorkflowInvoker and System.Activities.WorkflowApplication.WorkflowInvoker provides a simple model to execute the workflow like a method call whereas WorkflowApplication much greater flexibility to execute and manage the workflow instance. To start with we will explore the different mechanisms to pass data back and forth using WorkflowInvoker and WorkflowApplication.

(more…)

In the last post related to Roslyn we developed a very basic syntax walker. In this post we will see how we can use the same for code analysis and checking. As a good practice we generally try to avoid having instance members in a singleton class. This is true if you are making your business logic layer classes Singleton and would prefer to avoid mess up by multiple threads. In order to check that we need to check the following:

a) Whether the class has any instance fields or properties

b) Whether class is a singleton i.e having a private constructor

Let’s consider the following singleton class:

(more…)