Posts

Showing posts from March, 2009

Connection Strings at Run Time

If you have a scenario where you want to generate the connection string at runtime or you have the part of a connection string in config file and want to form the remaining part of it at run time. And sometimes, it’s the requirement saying not to store this kind of sensitive information in the configuration files. Using DbConnectionStringBuilder class you can achieve the same. Below are some of the useful Microsoft articles on this: http://msdn.microsoft.com/enus/library/system.data.common.dbconnectionstringbuilder(v=vs.80).aspx#Y1208 http://msdn.microsoft.com/en-us/library/ms254947(v=vs.80).aspx

Code Contracts in .Net 4.0

Introduction Microsoft Code Contacts is a very exciting new feature for creating reliable software and enhancing code quality by bringing the advantages of design-by-contract programming in all .NET programming languages. Contract Programming practice was first introduced by Bertrand Meyer in Eiffel programming language. The contracts classes’ new namespace System.Diagnostics.Contracts is in mscorlib of .NET 4.0, while .NET 3.5 has a small separate library named Microsoft.Contracts which needs an explicit reference. Benefits The Code Contracts provide users with a number of benefits: Contracts are declarative - The syntax for code contracts is standard, quite rich and variegated to express preconditions, postconditions, and invariants Contracts document the design decisions and facilitate code evaluation Improved testing - Code contracts provide static checking, runtime checking and documentation generation Contracts static verification helps · Early error detection and avoids pass

Mapping an XML to Business Objects using LINQ

Class Customer Private _iD As Integer Public Property ID() As Boolean Get Return _iD End Get Set(ByVal Value As Boolean) _iD = Value End Set End Property Private _name As String Public Property Name() As String Get Return _name End Get Set(ByVal Value As String) _name = Value End Set End Property Private _department As String Public Property Department() As String Get Return _department End Get Set(ByVal Value As String) _department = Value End Set End Property Private _orderList As New List(Of Order) Public Property OrderList() As List(Of Order) Get Me.LoadOrders() Return Me._orderList End Get Set(ByVal value As List(Of Order)) Me._orderList = value End Set End Property Private Sub LoadOrders() End

Nullable Types

This feature was added to C# 2005. This allows us to assign value ‘null’ to a value-type variables including struct. Lets take a scenario we have an ‘age’ field which is of type int in database, the value for this field could be all the values that can be assigned to value-type int plus it could be ‘null’ value (undefined or not used or empty). This is possible using nullable type variables; we can assign the exact value of age as it is in the database to our nullable-type variable. The nullable type is declared with the ? symbol just next to any value-type. Value-Type? myVariable; int age? = null; Nullable types are instances of the System.Nullable struct. The two important Properties of the Nullable struct are HasValue and Value. In the below example, age.HasValue returns false as there is no value assigned to age int value-type. using System; class MyClass { static void Main() { int? age; age = null; int simpleInt=-1; if (age.HasValue)

Attributes in C#

Attributes adds the information to the program entities such as an Assembly, Module, Class, Struct, Enum, Constructor, Method, Property, Field, Event, Interface, Parameter, Delegate. The information that Attribute contains is inserted into the assembly, at run time. The compiler can retrieve/queried this information at run-time using reflection. Using predefined attributes: There can be predefined or user-defined/custom attributes. Before digging into custom attributes we will have a quick look at using user-defined attributes, some of them are: System.AttributeUsageAttribute - Describe the ways in which an attribute class can be used, we are going to know how to use it in a few minutes in custom attributes section. System.Diagnostics.ConditionalAttribute - Used to define conditional methods. Conditional Attribute is used to create conditional methods; this method is being called if a specific preprocessor symbol is defined using #define, if not the method will be bypassed/resumed. T

Indexers

Indexers treats object as an array or collection, we can access the indexer’s elements using square brackets ([]) it enables an object to be indexed and provide array-like or collection-like access to the classes. Indexer defined much like properties. C# developers also called this as smart arrays. this [argument list] { get { // Get codes goes here } set { // Set codes goes here } } Eg. namespace ConsoleApplication1 { public class Customer { public int Id; public string Name; public string City; public Customer(int id, string name, string city) { Id = id; Name = name; City = city; } } class SampleCollection { // Declare an array to store the data elements. private T[] arr = new T[100]; // Define the indexer, which will allow client code // to use [] notation on the class instance itself. // (See line 2 of code in Main below.) public T this[int i] { get { // This indexer is very simple, and just returns or sets // the corresponding element from the internal array. return arr[i]; } set

Structs vs. Classes

Standard question which I would like to ask to the rejected candidates as one of the last questions to make them feel that they have not done very badly in their interview ;) A struct is a user-defined value type created on the stack. It may contain both reference and value types. It’s used to represent a light weight object which consumes a small memory such as point, color and the like. Unlike classes, the fields of a struct cannot be initialized. It will give an error saying ‘cannot have instance field initializers in structs’. struct MyStruct { int x = 0; // error int y = 0; // error } Structs cannot contain explicit parameter less constructors (default constructor), but we can have one or more constructors. C# compiler provides implicitly provides default constructor to initialize the default values for the members of a struct. struct MyStruct { int i,j; MyStruct() // error { //... } MyStruct(int i) //correct { this.i = i; this.j = 0; } MyStruct(int i, int j) //correct { this.i =