Skip to main content

Posts

Showing posts from July, 2015

How to get default value at runtime of a type

The problem is very simple and basic.
At runtime, I want to be able to get the default value of a specific type.
One solution is to use reflection and create a new instance of the object. But in this case you would use reflection more than one.

Let's analyze the problem. There two group of types that we could have: Value Type and Reference Type. For Reference type the solution is very simple, because we need all the time to return NULL. For Value Type we can use Activator class to create a new instance of our type.
public static class TypeExtension { public static object GetDefaultValue(this Type type) { if (type.IsValueType) { return Activator.CreateInstance(type); } return null; } } Assert.AreEqual(0,typeof(int).GetDefaultValue()); Assert.AreEqual(default(decimal), typeof(decimal).GetDefaultValue()); Assert.AreEqual(null, typeof(string).GetDefaultValue()); For this purpose I created an extension methods that returns the defa…

[Post-Event] 9-10 July, 2015 – Implementing effective web applications with Microsoft web stack

The 'Implementing effective web applications with Microsoft web stack' workshop ended a few days ago. We had two full days at the end of last week where ITCamp community members had the opportunity to learn and discover how we can develop web application using Microsoft technologies.
The two day event was held at City Plaza Hotel in Cluj-Napoca (July 9-10, 2015). At this event there were almost 60 people that had the opportunity to learn web programming secrets from Andrea Saltarello.
Different topics were discussed during this two days like:

SecurityASP.NET MVCAngularJSSOAWebAPIEntity FrameworkvNextRouting The trainer prepared a very interesting sample code for this workshop that can be found on CodePlex (nsk.codeplex.com). Below you can find some pictures from the event:














Lifecycle of Worker Roles (RoleEntryPoint)

In this post we will talk about 3 methods that are available in the RoleEntryPoint class for Web and Worker Roles (especially Worker Roles)

OnStartRunOnStop
This 3 methods are called in different moment of the lifecycle of a Worker Role. Each of them has a clear scope and output.

OnStart
This method is called automatically when the instance of Worker Role is initialized. It is used to initialized the context or prepare the instance before executing the task.
This method returns a bool value that it is used to tell the system if the initialization was made with success or not. On the happy case the return value should be true. If an error occurs or the initialization fails, that the false value should be returned. When OnStart methods returns false, the Run method will not be called and the instance will be 'restarted' - No other methods will be called.

Run
The Run method is used to execute the batch operation or the logic that is necessary to be executed by the Worker Role. We sh…

New features of C# 6.0

If you already had the opportunity to look over the new features of C# 6.0 than you may not be so interested to read this post. Otherwise, this post is perfect for you.
The main scope of this post is to take a look over new features of C# 6.0 that are already available and identify use cases where we can use them with success.

Auto-property initializer
Starting from now we will be able to set a default value of a property directly. We don't need anymore to initialize the value from constructor.
On top of this, we can set a default value for a properties that have only a getter, without a setter. In this way we can replace the read only fields that are public exposed.

public class Foo { public int Value { get; set; } = -1; public string Name { get; } = "Tom"; }
This is not all, on top of this, we can set the default value of a property in a constructor even if we don't have a setter. This is a big change from the old C# language.

public class Foo…