Code Heaven For C# Developer

Tuesday, December 20, 2011

Just for fun......

Labels:

Monday, December 19, 2011

Using CultureInfo class in C# to implement Globalization

CultureInfo class provides culture-specific information in .NET applications. This information includes language, sublanguage, country or region, day names, month names, calendar etc. It also provides culture specific conversions of numbers, currencies, dates or strings. In the following tutorial, I will show you how you can retrieve this information from different cultures available in .NET Framework.

To use CultureInfo class you need to import System.Globalization namespace which contains many classes used in the following code such as RegionInfo, DateTimeFormatInfo or NumberFormatInfo.

We are going to create the below form.




Following code is loading all specific cultures available in .NET Framework in the listbox.
private void Form1_Load(object sender, EventArgs e)
{
// Load All Specific Cultures
CultureInfo[] allCultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo culture in allCultures)
{
listBox1.Items.Add(culture.EnglishName );
}
}

When user select any culture from the listbox following code retrieve that culture, set current application and user interface culture and then display culture specific information on different labels.


private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
CultureInfo[] allCultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
int index = listBox1.SelectedIndex;

CultureInfo c = (CultureInfo)allCultures.GetValue(index);

// Set Current Application and User Interface Culture

System.Threading.Thread.CurrentThread.CurrentCulture = c;
System.Threading.Thread.CurrentThread.CurrentUICulture = c;

label1.Text = "Application Culture Name: " + c.Name;
label2.Text = "Application Culture Native Name: " + c.NativeName;
label3.Text = "Application Culture English Name: " + c.EnglishName;
label4.Text = "Application Culture Neutral Name: " + c.TwoLetterISOLanguageName;
label5.Text = "Application Culture Language Name: " + c.ThreeLetterISOLanguageName;
label6.Text = "Application Culture Windows Name: " + c.ThreeLetterWindowsLanguageName;

// RegionInfo Object

RegionInfo r = new RegionInfo(c.Name);

label7.Text = "Region English Name: " + r.CurrencyEnglishName;
label8.Text = "Currency Symbol: " + r.CurrencySymbol;
label9.Text = "Region English Name: " + r.EnglishName;

// DateTimeFormatInfo Object is used
// to get DayNames and MonthNames

string[] days = c.DateTimeFormat.DayNames;
listBox2.Items.Clear();
foreach (string day in days)
{
listBox2.Items.Add(day);
}

string[] months = c.DateTimeFormat.MonthNames;
listBox3.Items.Clear();
foreach (string month in months)
{
listBox3.Items.Add(month);
}

// Formatting Currency and Numbers

label10.Text = "Full Date Time Pattern: " + c.DateTimeFormat.FullDateTimePattern;
label11.Text = "Formatted Date: " + DateTime.Now.ToString("d", c.DateTimeFormat);
label12.Text = "Formatted Number: " + 65000.ToString("C", c.NumberFormat);


}
catch (Exception ex)
{ }
}

Labels:

Wednesday, December 14, 2011

Dispose vs Finalize in C#

Dispose/Finalized Pattern

.Microsoft recommends that you implement both Dispose and Finalize when working with unmanaged resources. The Finalize implementation would run and the resources would still be released when the object is garbage collected even if a developer neglected to call the Dispose method explicitly.
.Cleanup the unmanaged resources in the Finalize method and the managed ones in the Dispose method, when the Dispose/Finalize pattern has been used in your code.
.Structures and class can both implement IDisposable (unlike overriding Finalize(), which is for class types only)
.Dispose is never called by GS
.Disposed is executed as soon as you call it, and Finalized only when GC in a right mood for that.

class MyResourceWrapper:IDisposable
{
~MyResourceWrapper()
{
//Clean up unmanaged resources here
Console.WriteLine("MyResourceWrapper is finalized");
}
public void Dispose()
{
//Clean up unmanaged resources here
Console.WriteLine("MyResourceWrapper is disposed");
}
}
class ClassWithoutAnyting
{
}

static void Main(string[] args)
{
MyResourceWrapper ForFinalizeOnly = new MyResourceWrapper();
ForFinalizeOnly = null;
MyResourceWrapper ForFinalizeAndDispose = new MyResourceWrapper();
ForFinalizeAndDispose.Dispose();
ForFinalizeAndDispose = null;
//If you are bored to type Disposed, you can type "using" instead
using (MyResourceWrapper ForFinalizeAndDispose1 = new MyResourceWrapper()){}
//There is a compile error if you utilizing "using" with object
//without Dispose method implemented
//*** Compile ERROR!
using (ClassWithoutAnyting newclass = new ClassWithoutAnyting()){}

GC.Collect();
GC.WaitForPendingFinalizers();
}

Labels: