Sunday, August 8, 2010

What is Globalization?

Hello Friends,
We often came across the problems related to the geographical scope of applications. The costs of not knowing who will be using the applications are increasing as well. Dealing with issues as an afterthought is always more costly than designing for them, and building applications for different locales is no different in this respect. The .NET Framework provides the System.Globalization namespace to help developers address such concerns.

CultureInfo Class
One of the core tools for manipulating and retrieving information about the cultural context an application is running in is the CultureInfo class. This class provides culture-specific information such as the format of numbers and dates, and casing conventions. More broadly, it represents the name of a culture, the culture's writing system, the culture's calendar, the culture's language and sublanguages if applicable, and the country and region of the culture, and it provides methods to manipulate all these aspects. The basic uses of the CultureInfo class are shown in the following list:

Control how string comparisons are performed.

Control how number comparisons and formats are performed.

Control how date comparisons and formats are performed.

Control how resources are retrieved and used.

As a rule, a culture will be grouped into one of three categories: an invariant culture, a neutral culture, or a specific culture. The distinctions between these categories are detailed in the following list:

Invariant Culture This culture category is culture-insensitive. The category is to be used as essentially a default culture when consistency is desired. One situation where this category might be desirable to use is creating a trial application with a hard-coded expiration date. Using an invariant will allow you to check for a specific date, regardless of the culture's format, which will greatly simplify the task of comparing these dates. The invariant culture is not based on the English language per se, but it is associated with it and bears more similarities to it than to any other identifiable culture. Although it might be tempting to use the invariant culture for every possible comparison and just ignore specific cultural comparisons, doing so is a great mistake. Without intending to do so, you can overuse an invariant culture and end up with language that is syntactically incorrect or inappropriate.

Neutral Culture English (en), French (fr), and Spanish (sp) are all neutral cultures. A neutral culture is associated with a language but has no relationship to countries or regions. For instance, the English spoken in England is different from that spoken in the United States. The same holds true for Spanish spoken in Mexico versus that spoken in Spain. As mentioned earlier, the neutral culture will be designated by the first two characters in the CultureInfo class. If only two letters are specified, they will be the Neutral class. Although neutral cultures, like the invariant culture, might be tempting to use, they should be avoided as well, if possible, for the same reasons that invariants should be avoided. The language spoken in different countries that are covered by a neutral culture will almost certainly be different in at least a few respects. In reality, the differences will be many. Therefore, overuse of neutral cultures can result in incorrect or inappropriate language.

Specific Culture This is the most precise of the three categories and is represented by a neutral culture, a hyphen, and then a specific culture abbreviation. For instance, in the designations "fr-FR" and "en-US", fr and en represent the neutral culture (French and English, respectively), and FR and US represent the specific culture (France and the United States, respectively). Specific cultures should be used if at all possible.

To detect a user's current culture information, use the CurrentCulture property of the executing thread's CurrentThread property, as shown in the following code sample:

CultureInfo UsersCulture = Thread.CurrentThread.CurrentCulture;
MessageBox.Show("The current culture of this application is : " + UsersCulture.Name);
MessageBox.Show("The Display Name of this application is : "
+ UsersCulture.DisplayName);
MessageBox.Show("The Native Name of this application is : "
+ UsersCulture.NativeName);
MessageBox.Show("The ISO Abbreviation of this application is : "
+ UsersCulture.TwoLetterISOLanguageName);


Setting the current culture is similar to retrieving it. CurrentCulture is a property of the CurrentThread property of the Thread class; therefore, all that's needed to change or set this property is to specify a different value for it, as shown in the following code:

// C#
CultureInfo UsersCulture = Thread.CurrentThread.CurrentCulture;
MessageBox.Show("The current culture of this application is : "
+ UsersCulture.Name);
//Change this to Spanish/Venezuela
Thread.CurrentThread.CurrentCulture = new CultureInfo("es-VE");
MessageBox.Show("The current culture of this application is : "
+ Thread.CurrentThread.CurrentCulture);


Thanks,
Paras Sanghani

What are Generics?

Hello Friends,

Generics are part of the .NET Framework's type system that allows you to define a type while leaving some details unspecified. Instead of specifying the types of parameters or member classes, you can allow code that uses your type to specify it. This allows consumer code to tailor your type to its own specific needs.

The .NET Framework version 2.0 includes several generic classes in the System.Collections.Generic namespace, including Dictionary, Queue, SortedDictionary, and SortedList. These classes work similarly to their nongeneric counterparts in System.Collections, but they offer improved performance and type safety.


Why Use Generics?
Versions 1.0 and 1.1 of the .NET Framework did not support generics. Instead, developers used the Object class for parameters and members and would cast other classes to and from the Object class. Generics offer two significant advantages over using the Object class:

Reduced run-time errors:
The compiler cannot detect type errors when you cast to and from the Object class. For example, if you cast a string to an Object class and then attempt to cast that Object to an integer, the compiler will not catch the error. Instead, the runtime will throw an exception. Using generics allows the compiler to catch this type of bug before your program runs. Additionally, you can specify constraints to limit the classes used in a generic, enabling the compiler to detect an incompatible type.

Improved performance
Casting requires boxing and which steals processor time and slows performance. Using generics doesn't require casting or boxing, which improves run-time performance.

How to Create a Generic Type
First, examine the following classes. Classes Obj and Gen perform exactly the same tasks, but Obj uses the Object class to enable any type to be passed, while Gen uses generics:

// C#
class Obj
{
public Object t;
public Object u;

public Obj(Object _t, Object _u)
{
t = _t;
u = _u;
}
}

class Gen
{
public T t;
public U u;

public Gen(T _t, U _u)
{
t = _t;
u = _u;
}
}

As you can see, the Obj class has two members of type Object. The Gen class has two members of type T and U. The consuming code will determine the types for T and U. Depending on how the consuming code uses the Gen class, T and U could be a string, an int, a custom class, or any combination thereof.

There is a significant limitation to creating a generic class: generic code is valid only if it will compile for every possible constructed instance of the generic, whether an Int, a string, or any other class. Essentially, you are limited to the capabilities of the base Object class when writing generic code. Therefore, you could call the ToString or GetHashCode method within your class, but you could not use the + or > operator. These same restrictions do not apply to the consuming code because the consuming code has declared a type for the generic.

How to Use Constraints
Generics would be extremely limited if you could only write code that would compile for any class, because you would be limited to the capabilities of the base Object class. To overcome this limitation, use constraints to place requirements on the types that consuming code can substitute for your generic.

Generics support four types of constraints:

Interface Allow only types that implement specific interfaces to use your generic.

Base class Allow only types that match or inherit from a specific base class to use your generic.

Constructor Require types that use your generic to implement a parameterless constructor.

Reference or value type Require types that use your generic to be either a reference or value type.

Use the As clause in Visual Basic or the where clause in C# to apply a constraint to a generic. For example, the following generic class could be used only by types that implement the IComparable interface:


// C#
class CompGen where T : IComparable
{
public T t1;
public T t2;


public CompGen(T _t1, T _t2)
{
t1 = _t1;
t2 = _t2;
}

public T Max()
{
if (t2.CompareTo(t1) < 0)
return t1;
else
return t2;
}
}

The preceding class will compile correctly. However, if you remove the where clause, the compiler will return an error indicating that generic type T does not contain a definition for CompareTo. By constraining the generic to classes that implement IComparable, you guarantee that the CompareTo method will always be available.

Thanks,
Paras Sanghani