Socket programming vs. WCF

By , May 31, 2011

A very simple scenario where client sends a request to server to perform certain action. There is no much data transfer between two parties – Probably no more than 50 bytes. We need to decide whether we go with Socket programming or use WCF (request-reply messaging pattern).

The analysis says: Read more »

Quick and short: Database Indexes

By , May 31, 2011

This article is written under ‘Quick and Short’ edition. In this article I will explain about database indexes, type of indexes, best practices to use index, how to use indexes to improve performance of T-SQL (database query).

Read more Quick and Short articles.

What is index

Indexes in databases are very similar to indexes in libraries. Indexes allow locating information within a database fast, much like they do in libraries. If all books in a library are indexed alphabetically then you don’t need to browse the whole library to find particular book. Instead you’ll simply get the first letter from the book title and you’ll find this letter’s section in the library starting your search from there, which will narrow down your search significantly.

An Index can be created on a single column or a combination of columns in a database table. A table index is a database structure that arranges the values of one or more columns in a database table in specific order. The table index has pointers to the values stored in specified column or combination of columns of the table. These pointers are ordered depending on the sort order specified in the index. Read more »

.Net Framework 4.0: The Background and Foreground GC

By , May 31, 2011

In my previous article I have explained about concurrent GC for workstation version of the CLR (Differences between Server and Workstation GC (types of garbage collection)). The CLR 4.0 has come with some performance enhancement (called “Background GC”) on the memory management process. This enhancement is majorly done with concurrent GC processing.

The Background GC came in picture because there was some latency issue with concurrent GC. Let me try to explain about the issue:

As you remember, Concurrent GC means the GC thread will run in parallel without blocking the application execution (well, not exactly the parallel – it was just minimize the blocking time). The concurrent GC operation can take some time if the memory allocation is quite large and this prevents short-lived (gen 0 and 1) collections while is running. Read more »

Differences between Server and Workstation GC (types of garbage collection)

By , May 30, 2011

In my previous article (Garbage Collection – up to .Net Framework 3.5) I explained basic concept of GC. Here I am going deep to explain more about types of GC provided by CLR.

The CLR provides two types of garbage collection 1) Workstation garbage collection and 2) Server garbage collection.

Several times I have heard one common question in the community asking the differences between server and workstation GC, and how Concurrent GC fits in-between. Let me try to explain here:

Server GC: It is only available on multi-proc machines. It creates one GC heap (and thus one GC thread) for each processor, which are collected in parallel. This GC mode maximizes throughput (number of requests per second) and shows good scalability (performance really shines with 4 or more processors). Read more »

Garbage Collection: .Net Framework 3.5

By , May 29, 2011

Here I am covering some basic information about how GC works in CLR. More detailed informations are covered @

 

 

The garbage collector is an automatic memory manager. It provides the following benefits:

  • Enables us to develop the application without having to free memory.
  • Allocates objects efficiently on the managed heap.
  • Reclaims no longer used objects, clears their memory, and keeps the memory for future allocations.
  • Provides memory safety by making sure that an object cannot use the content of another object. Read more »

Why to migration from .net 1.1 to the latest framework

By , May 29, 2011

Some days back one of my readers asking me a question about why one should migration from .net 1.1 to the latest framework. Here is the part of discussion:

Reader: Why the product should be migrated from .net Framework 1.1 to later framework if product is working as expected without any issue. What are different advantages (performance and system stability) regarding the upgrade to higher versions of the framework? What can be a good reason for the client to allow us to work on migration?

Me: Migrating existing .Net application developed using older version of .Net framework to the latest version is always worth considering, for many reasons. .Net application should be upgraded from time to time to the latest version of .Net to accrue the maximum benefits from technology advancement. Some of the reason/benefits are as follow: Read more »

Interface vs Abstract class

By , April 20, 2011

An Abstract class without any implementation just looks like an Interface; however there are lot of differences than similarities between an Abstract class and an Interface. Let’s explain both concepts and compare their similarities and differences.

What is an Abstract Class?

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

What is an Interface?

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.

Both Together

When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.

When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.

There are some similarities and differences between an interface and an abstract class that I have arranged in a table for easier comparison:

Feature Interface Abstract class
Multiple inheritance A class may inherit several interfaces. A class may inherit only one abstract class.
Default implementation An interface cannot provide any code, just the signature. An abstract class can provide complete, default code and/or just the details that have to be overridden.
Access Modfiers An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public An abstract class can contain access modifiers for the subs, functions, properties
Core VS Peripheral Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface. An abstract class defines the core identity of a class and there it is used for objects of the same type.
Homogeneity If various implementations only share method signatures then it is better to use Interfaces. If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.
Speed Requires more time to find the actual method in the corresponding classes. Fast
Adding functionality (Versioning) If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
Fields and Constants No fields can be defined in interfaces An abstract class can have fields and constrants defined
Is-a vs able or can-do Interfaces are often used to describe the peripheral abilities of a class, not its central identity, e.g. an Automobile class might implement the Recyclable interface, which could apply to many otherwise totally unrelated objects. An abstract class defines the core identity of its descendants. If you defined a Dog abstract class then Damamation descendants are Dogs, they are not merely dogable. Implemented interfaces enumerate the general things a class can do, not the things a class is.
Homogeneity If all the various implementations share is the method signatures, then an interface works best. If the various implementations are all of a kind and share a common status and behavior, usually an abstract class works best.
Maintenance If your client code talks only in terms of an interface, you can easily change the concrete implementation behind it, using a factory method. Just like an interface, if your client code talks only in terms of an abstract class, you can easily change the concrete implementation behind it, using a factory method.
Adding functionality If you add a new method to an interface, you must track down all implementations of that interface in the universe and provide them with a concrete implementation of that method. If you add a new method to an abstract class, you have the option of providing a default implementation of it. Then all existing code will continue to work without change.

Understanding Destructors in C#

By , April 14, 2011

Introduction

This article is about understanding the working concept of destructor in C#. I know you all may be thinking why a dedicated article on simple destructor phenomenon. As you read this article you will understand how different is C# destructor are when compared to C++ destructors.

In simple terms a destructor is a member that implements the actions required to destruct an instance of a class. The destructors enable the runtime system, to recover the heap space, to terminate file I/O that is associated with the removed class instance, or to perform both operations. For better understanding purpose I will compare C++ destructors with C# destructors. Read more »

C#: lock vs Monitor

By , April 12, 2011

They both are same but each one has some draw back and advantages.

lock is more concise over monitor; it releases the lock from the blocked code even if there is exception generated within blocked code. There could be deadlock with lock if we do not use it correctly, also when we use lock the thread will wait for infinite time to get the lock on object. Read more »

Events Programming in C# and .NET

By , March 13, 2011

In the early days of computing, a program would begin execution and then proceed through its steps until it completed. If the user was involved, the interaction was strictly controlled and limited to filling in fields.

Today’s Graphical User Interface (GUI) programming model requires a different approach, known as event-driven programming. A modern program presents the user interface and waits for the user to take an action. The user might take many different actions, such as choosing among menu selections, pushing buttons, updating text fields, clicking icons, and so forth. Each action causes an event to be raised. Other events can be raised without direct user action, such as events that correspond to timer ticks of the internal clock, email being received, file-copy operations completing, etc. In programming, you are often faced with situations where you need to execute a particular action, but you don’t know in advance which method, or even which object, you’ll want to call upon to execute it. For example, a button might know that it must notify some object when it is pushed, but it might not know which object or objects need to be notified.

C# adds on value to the often mentioned world of event driven programming by adding support through events and delegates. Read more »

How to improve garbage collection performance

By , March 6, 2011

Introduction
DotNet developers can free themselves from tedious memory management for their application as Microsoft Framework and CLR do it automatically.CLR provides a mechanism called as Garbage Collection which manages your applications memory. In this session we will discuss   how garbage collector works and how it affects the performance of your Applications.

When you create an object using new () operator, the object’s memory is obtained from the managed heap. When the garbage collector decides that sufficient garbage has accumulated, it performs a collection to free some memory. This process is fully automatic, but there are a number of factors that you need to be aware of that can make the process more or less efficient. Read more »

Introduction to Objects and Classes in C#

By , March 1, 2011

In this article we will understand some of the concepts of object-oriented programming in C# like objects and classes. To read this article you must have C# programming basics.

Read more »

WCF – Authenticate the Client using User Name and Password

By , February 11, 2011

WCF is a distributed technology, you build the WCF service to serve the functionality to the Client, The Client could be on Intranet or on Internet. It is almost always mandatory for the Service to know its client (Authenticate) before accepting the message or sending the message to the client.

WCF provides various ways to accept the credential from client and authenticate it. The following table shows the possible credential types that you can use when creating an application. You can use these values in either code or configuration files. Read more »

WCF – Security Overview (Fundamental)

By , February 9, 2011

Windows Communication Foundation – Why security is needed?

Using WCF, we can create applications that function as both services and service clients. One service could be transmitting, creating and processing messages for an unlimited number of other services and clients. In such a distributed application, messages can flow from node to node, through firewalls, onto the Internet, and through numerous SOAP intermediaries. This introduces a variety of message security threats. The following examples illustrate some common threats that WCF security can help mitigate when exchanging messages between entities: Read more »

WCF: Target Framework Attribute

By , February 8, 2011

With .Net Framework 4.0, the target framework attribute is introduced to specify the version of the .NET Framework for the application hosted in IIS or WAS is targeting. It allows you to build applications that target .NET Framework 2.0, 3.5, or 4.0 using Visual Studio. This attribute set within a <compilation> tag in an application’s Web.config file as shown in the following example.

<compilation debug=”false”  targetFramework=”4.0″>

<assemblies>

<add assembly=”System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089″/>

<add assembly=”System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089″/>

<add assembly=”System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″/>

<add assembly=”System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089″/>

</assemblies>

</compilation> Read more »