Editorials

C# Constructor Newbie Question

$$SWYNK$$

Featured Article(s)
How to Backup (Part 1)
Since data is the lifeline of your company, you, as a Data Professional, need to ensure that your data is protected, recoverable, usable, secure, and so on. One of the first tasks that you should perform as a Data Professional is to ensure that the data is properly backed up. This seems like a simple task. There are as many ways to perform this task as you could imagine. Your backup plan can range from the very simple to extremely complex.

Featured White Paper(s)
Demystify Tempdb Performance and Management
In this white paper, Robert L Davis demystifies the best practices that work some of the time, but not all of the time, when … (read more)

C# Constructor Newbie Question
In C# what does it mean when a method is completely empty? e.g. public class Notification { public Notification() {} other methods defined…… }

In C# there are a number of extension classes that will work with your class as long as it has an Anonymous Constructor. An Anonymous Constructor is a constructor that may be called when your class is instantiated, not requiring input parameters.

By default, every class you create automatically has a default constructor, unless you create any other constructor having parameters. In this case, in order for other utilities requiring an anonymous constructor to use your class, you must define your own anonymous constructor. If there is nothing to do in this constructor, ie. you don’t have any properties or private variables to set the default state, then the anonymous constructor method is completely empty.

One example of a utility requiring your class to have a default constructor is the XML Serializer or the JSON serializer methods. These helper classes will create a default instance of your class (calling only the default constructor) and populate the public properties.

A default class constructor is a good practice in most circumstances. If you wish, an initialize or reset method is often appropriate as well, setting or resetting the state of your class to default settings. Having these methods available, any constructor implemented in your class may call the initialize or reset method prior to doing any work.

If you are doing any CLR programming in SQL Server, it is a great habit to get into where you initialize all your private variables upon instantiation so there is no concern regarding the state of your object.

Drop a note to btaylor@sswug.org if you would like to comment on this tip, request other questions, add your own advice, or even contribute other suggestions for our readers.

Cheers,

Ben