Wednesday, May 25, 2011
Registry Hacks to Make Your PC Faster
http://www.pcworld.in/how-to/6-registry-hacks-make-your-pc-faster
Monday, May 23, 2011
Latest way of debugging using Debugger Canvas
There is a latest way of debugging in VS please read this
Thursday, May 19, 2011
Wednesday, May 18, 2011
Lazy in C#
If you want to initialize a class as lazy Initialization you can go with Lazy key word.
Lazy _expensive = new Lazy
(() => new Expensive(), true);
Note: If you pass true as parameter,it will be thread safe else as false, it will not be thread safe.
If for a single -thread or single ton class Lazy is best
Lazy
(() => new Expensive(), true);
Note: If you pass true as parameter,it will be thread safe else as false, it will not be thread safe.
If for a single -thread or single ton class Lazy is best
If you want to return a query with Default if its Empty?
If you want to return a query with default value if it is empty you can go with DefaultIfEmpty
void Main()
{
Coder coder = new Coder("Jon");
List coderCollection = new List()
{
new Coder("Anish"),
new Coder("marokey"),
};
var result = coderCollection.Where(r => r.Name == "Pat").DefaultIfEmpty(coder);
}
// Define other methods and classes here
class Coder
{
public string Name;
public Coder(string Name)
{
this.Name = Name;
}
}
void Main()
{
Coder coder = new Coder("Jon");
List
{
new Coder("Anish"),
new Coder("marokey"),
};
var result = coderCollection.Where(r => r.Name == "Pat").DefaultIfEmpty(coder);
}
// Define other methods and classes here
class Coder
{
public string Name;
public Coder(string Name)
{
this.Name = Name;
}
}
Order by with out Culture
If you want to do some operations without any culture you can do with the help of StringCompare.Ordinal or StringCompare.OrdinalIgnoreCase.
void Main()
{
string[] nonCulture = { "äax", "ääü", "äbü" };
IEnumerable orderNonCulture = nonCulture.OrderBy( r => r, StringComparer.Ordinal);
orderNonCulture.Dump();
}
void Main()
{
string[] nonCulture = { "äax", "ääü", "äbü" };
IEnumerable
orderNonCulture.Dump();
}
Monday, May 16, 2011
Sunday, May 15, 2011
Semaphore and SemaphoreSlim
If need to restrict number of thread, can go with Semaphore.
Here is the example
class TheClub // No door lists!
{
static SemaphoreSlim _sem = new SemaphoreSlim (3); // Capacity of 3
static void Main()
{
for (int i = 1; i <= 5; i++) new Thread (Enter).Start (i);
}
static void Enter (object id)
{
Console.WriteLine (id + " wants to enter");
_sem.Wait();
Console.WriteLine (id + " is in!"); // Only three threads
Thread.Sleep (1000 * (int) id); // can be here at
Console.WriteLine (id + " is leaving"); // a time.
_sem.Release();
}
}
SemaphoreSlim is introduced in .Net 4.0 and is much faster and simple
Here is the example
class TheClub // No door lists!
{
static SemaphoreSlim _sem = new SemaphoreSlim (3); // Capacity of 3
static void Main()
{
for (int i = 1; i <= 5; i++) new Thread (Enter).Start (i);
}
static void Enter (object id)
{
Console.WriteLine (id + " wants to enter");
_sem.Wait();
Console.WriteLine (id + " is in!"); // Only three threads
Thread.Sleep (1000 * (int) id); // can be here at
Console.WriteLine (id + " is leaving"); // a time.
_sem.Release();
}
}
SemaphoreSlim is introduced in .Net 4.0 and is much faster and simple
?? Operator used in Linq
Here is the Msdn link for ?? Operator
if need to order by a query which need to orderby column1 if not null, if null orderby column2
void Main()
{
List orderList= new List()
{
new Order(1, 4),
new Order(null,6),
new Order(2,5),
new Order(null,3),
};
var k = from p in orderList
orderby p.ID ?? p.Count
select p;
}
class Order
{
public int? ID{get;set;}
public int? Count{get;set;}
public Order(int? id,int? count)
{
ID =id;
Count =count;
}
}
if need to order by a query which need to orderby column1 if not null, if null orderby column2
void Main()
{
List
{
new Order(1, 4),
new Order(null,6),
new Order(2,5),
new Order(null,3),
};
var k = from p in
orderby p.ID ?? p.Count
select p;
}
class Order
{
public int? ID{get;set;}
public int? Count{get;set;}
public Order(int? id,int? count)
{
ID =id;
Count =count;
}
}
A simple example to say the difference between Descendants vs Elements
Here is the XML
if use Descendants
XDocument xDoc = XDocument.Load(@"C:\Users\anishmarokey\Desktop\XMLFile1.xml");
var result = from book in xDoc.Descendants("Book")
select new
{
Title = book.Attribute("Title"),
Author = book.Attribute("Author")
};
if use Elements
XDocument xDoc = XDocument.Load(@"C:\Users\anishmarokey\Desktop\XMLFile1.xml");
var result = from book in xDoc.Elements("Titles").Elements("Book")
select new
{
Title = book.Attribute("Title"),
Author = book.Attribute("Author")
};
if use Descendants
XDocument xDoc = XDocument.Load(@"C:\Users\anishmarokey\Desktop\XMLFile1.xml");
var result = from book in xDoc.Descendants("Book")
select new
{
Title = book.Attribute("Title"),
Author = book.Attribute("Author")
};
if use Elements
XDocument xDoc = XDocument.Load(@"C:\Users\anishmarokey\Desktop\XMLFile1.xml");
var result = from book in xDoc.Elements("Titles").Elements("Book")
select new
{
Title = book.Attribute("Title"),
Author = book.Attribute("Author")
};
Sort a class Employee
Here is a simple example for sorting an Employee class
public class MainClassToSortEmployee
{
public enum SortDirection { Ascending, Decending }
public static List Sort1(ref List list,
Func sorter, SortDirection direction)
{
if (direction == SortDirection.Ascending)
return list = list.OrderBy(sorter).ToList();
else
return list = list.OrderByDescending(sorter).ToList();
}
static void Main()
{
}
}
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime dateTime { get; set; }
public Employee(string p, string p_2, DateTime dateTime)
{
this.FirstName = p;
this.LastName = p_2;
this.dateTime = dateTime;
}
}
next task is to convert it to a Generic Linq
public class MainClassToSortEmployee
{
public enum SortDirection { Ascending, Decending }
public static List
Func
{
if (direction == SortDirection.Ascending)
return list = list.OrderBy(sorter).ToList();
else
return list = list.OrderByDescending(sorter).ToList();
}
static void Main()
{
}
}
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime dateTime { get; set; }
public Employee(string p, string p_2, DateTime dateTime)
{
this.FirstName = p;
this.LastName = p_2;
this.dateTime = dateTime;
}
}
next task is to convert it to a Generic Linq
Wednesday, May 11, 2011
Concat vs Union in C#
List list1 = new List(){1,2,3};
List list2 = new List(){1,2,4};
var dummyConcat = list1.Concat(list2); // combine all values without distinct
var dummyUnion = list1.Union(list2);// combine all values with distinct
result for dummyConcat
result for dummyUnion
List
var dummyConcat = list1.Concat(list2); // combine all values without distinct
var dummyUnion = list1.Union(list2);// combine all values with distinct
result for dummyConcat
result for dummyUnion
Convert IEnumerable to DataTable
Here is small example to convert IEnumerable to DataTable by using the keyword CopyToDataTable()
DataTable dt = new DataTable();
//Adding Columns and names
dt.Columns.Add("ID" ,typeof(int));
dt.Columns.Add("Name",typeof(string));
dt.Columns.Add("Rank",typeof(string));
//Adding Rows and values
dt.Rows.Add(1,"anish","First");
dt.Rows.Add(2,"marokey","First");
dt.Rows.Add(2,"varghese","Second");
//querying the result where ID is 2
IEnumerable result = from p in dt.AsEnumerable()
where p.Field("ID") == 2
select p;
// By using the keyword CopyToDataTable(.net 3.5 SP1 and above) converting the IEnumerable to DataTable
DataTable dtConvertedFromLinq = result.CopyToDataTable();
DataTable dt = new DataTable();
//Adding Columns and names
dt.Columns.Add("ID" ,typeof(int));
dt.Columns.Add("Name",typeof(string));
dt.Columns.Add("Rank",typeof(string));
//Adding Rows and values
dt.Rows.Add(1,"anish","First");
dt.Rows.Add(2,"marokey","First");
dt.Rows.Add(2,"varghese","Second");
//querying the result where ID is 2
IEnumerable
where p.Field
select p;
// By using the keyword CopyToDataTable(.net 3.5 SP1 and above) converting the IEnumerable to DataTable
DataTable dtConvertedFromLinq = result.CopyToDataTable
Why Field is required for DataTable Linq
Here is one good blog for the same Click here
E.g
DataTable dt = new DataTable();
//Adding Columns and names
dt.Columns.Add("ID" ,typeof(int));
dt.Columns.Add("Name",typeof(string));
//Adding Rows and values
dt.Rows.Add(1,"anish");
dt.Rows.Add(2,"marokey");
dt.Rows.Add(2,"varghese");
//querying the result where ID is 2
var result = from p in dt.AsEnumerable()
where p.Field("ID") == 2
select p.Field("Name");
E.g
DataTable dt = new DataTable();
//Adding Columns and names
dt.Columns.Add("ID" ,typeof(int));
dt.Columns.Add("Name",typeof(string));
//Adding Rows and values
dt.Rows.Add(1,"anish");
dt.Rows.Add(2,"marokey");
dt.Rows.Add(2,"varghese");
//querying the result where ID is 2
var result = from p in dt.AsEnumerable()
where p.Field
select p.Field
Add Columns and Name to DataTable
Here is a simple example to add values to DataTable
DataTable dt = new DataTable();
//Adding Columns and names
dt.Columns.Add("ID" ,typeof(int));
dt.Columns.Add("Name",typeof(string));
//Adding Rows and values
dt.Rows.Add(1,"anish");
dt.Rows.Add(2,"marokey");
DataTable dt = new DataTable();
//Adding Columns and names
dt.Columns.Add("ID" ,typeof(int));
dt.Columns.Add("Name",typeof(string));
//Adding Rows and values
dt.Rows.Add(1,"anish");
dt.Rows.Add(2,"marokey");
Some good APIs for PDF converter in .Net
Here is the some good APIs for PDF Converter
http://www.aspose.com/categories/.net-components/aspose.pdf-for-.net/default.aspx
http://sourceforge.net/projects/itextsharp/
http://www.aspose.com/categories/.net-components/aspose.pdf-for-.net/default.aspx
http://sourceforge.net/projects/itextsharp/
Tuesday, May 10, 2011
what is the use of parms keyword in C#
here is the msdn link for params
E.g:
with params
void Main()
{
Method(1,2,3); // you can call it like this
}
static void Method(params int[] array)
{
for(int i=0;i < array.Length;i++)
{
Console.WriteLine(array[i]);
}
}
without params
void Main()
{
Method(1,2,3); // throws error
}
static void Method (int[] array)
{
for(int i=0;i < array.Length;i++)
{
Console.WriteLine(array[i]);
}
}
you have to do
void Main()
{
int[] i = {1,2,3};
Method(i);
}
static void Method (int[] array)
{
for(int i=0;i < array.Length;i++)
{
Console.WriteLine(array[i]);
}
}
E.g:
with params
void Main()
{
Method(1,2,3); // you can call it like this
}
static void Method(params int[] array)
{
for(int i=0;i < array.Length;i++)
{
Console.WriteLine(array[i]);
}
}
without params
void Main()
{
Method(1,2,3); // throws error
}
static void Method (int[] array)
{
for(int i=0;i < array.Length;i++)
{
Console.WriteLine(array[i]);
}
}
you have to do
void Main()
{
int[] i = {1,2,3};
Method(i);
}
static void Method (int[] array)
{
for(int i=0;i < array.Length;i++)
{
Console.WriteLine(array[i]);
}
}
why the entry point of C# should be static void Main()
here is a couple of links which says the answer properly
Msdn: http://msdn.microsoft.com/en-us/library/acy3edy3.aspx
SO:http://stackoverflow.com/questions/5954439/c-entry-point-function
why the method name should be Main()
The using System; directive references a namespace called System that is provided by the Common
Language Infrastructure (CLI) class library. This namespace contains the Console class referred to in
the Main method.
taken from ecma-international
Msdn: http://msdn.microsoft.com/en-us/library/acy3edy3.aspx
SO:http://stackoverflow.com/questions/5954439/c-entry-point-function
why the method name should be Main()
The using System; directive references a namespace called System that is provided by the Common
Language Infrastructure (CLI) class library. This namespace contains the Console class referred to in
the Main method.
taken from ecma-international
Dynamic keyword in C#
if you want to set the variable at runtime go with dynamic
if you want to set the variable at compile time go with var
e.g in dynamic
void Main()
{
var k = exampleMethod("23");
Console.WriteLine(k);
}
public static dynamic exampleMethod(dynamic d)
{
dynamic local ="string";
int i =3;
if(d is string )
return local;
else
return i;
}
if you want to set the variable at compile time go with var
e.g in dynamic
void Main()
{
var k = exampleMethod("23");
Console.WriteLine(k);
}
public static dynamic exampleMethod(dynamic d)
{
dynamic local ="string";
int i =3;
if(d is string )
return local;
else
return i;
}
Tuple in C#
in .net 4.0 Tuple keyword is introduced, this is new to C# and old to sql, F# etc,
Mostly tuple is used to return multiple values with out using ref or out key word.
Public Tuple ReturnMultipleObjects()
{
Tuple multipleObjects = Tuple.Create("string",1, new TextBox());
return multipleObjects ;
}
Msdn Link: http://msdn.microsoft.com/en-us/library/system.tuple.aspx
So Question: http://stackoverflow.com/questions/5951808/c-defined-arrays
Code Project: http://www.codeproject.com/KB/cs/C_Sharp_4_Tuples.aspx
Mostly tuple is used to return multiple values with out using ref or out key word.
Public Tuple
{
Tuple
return multipleObjects ;
}
Msdn Link: http://msdn.microsoft.com/en-us/library/system.tuple.aspx
So Question: http://stackoverflow.com/questions/5951808/c-defined-arrays
Code Project: http://www.codeproject.com/KB/cs/C_Sharp_4_Tuples.aspx
Hello World in Android using MONO
Here is a good article which helps to write a Hello World using MONO
http://mono-android.net/Tutorials/Hello_World
http://mono-android.net/Tutorials/Hello_World
Monday, May 9, 2011
Event Based Async Pattern(EAP)
The event-based asynchronous pattern (EAP) provides a simple means by which classes can offer multithreading capability without consumers needing to explicitly start or manage threads.
E.g: BackgroundThread, WebClient in System.Net
E.g: BackgroundThread, WebClient in System.Net
yield vs return in C#
The yield keyword signals to the compiler that the method in which it appears is an iterator block.
void Main()
{
foreach(int i in GetDivideByTwo())
{
Console.WriteLine(i);
}
}
static System.Collections.Generic.IEnumerable GetDivideByTwo()
{
for (int i = 0; i < 100; i++)
yield return i/2;
}
Here is couple of good links
http://stackoverflow.com/questions/410026/c-proper-use-of-yield-return
http://stackoverflow.com/questions/1088442/what-is-the-purpose-advantage-of-using-yield-return-iterators-in-c/1088452#1088452
http://stackoverflow.com/questions/3969963/when-not-to-use-yield-return/3970171#3970171
- yield can be used to return partially.
- yield maintains the state of the object
void Main()
{
foreach(int i in GetDivideByTwo())
{
Console.WriteLine(i);
}
}
static System.Collections.Generic.IEnumerable
{
for (int i = 0; i < 100; i++)
yield return i/2;
}
Here is couple of good links
http://stackoverflow.com/questions/1088442/what-is-the-purpose-advantage-of-using-yield-return-iterators-in-c/1088452#1088452
http://stackoverflow.com/questions/3969963/when-not-to-use-yield-return/3970171#3970171
Saturday, May 7, 2011
Why Visual Studio takeing too much time to attach a Process
The reason this is simple, when you are added many break points and attach the process in the Debugger, it will take too much time to load all the controls. so better remove all the breakpoints and attach, wont take much time
ASP.NET Patterns every developer should know
Here is a good link for Design patterns in Asp.Net
http://www.developerfusion.com/article/8307/aspnet-patterns-every-developer-should-know/
http://www.developerfusion.com/article/8307/aspnet-patterns-every-developer-should-know/
Thursday, May 5, 2011
Declare and Intiaize Dictionary in a single line
This is a way to initialize the Dictionary using Collection intializers , specify one or more element intializers when you initialize a collection class that implements IEnumerable
Dictionary dic = new Dictionary()
{
{"erer","er"},
{ "rrr","rr"}
};
Dictionary
{
{"erer","er"},
{ "rrr","rr"}
};
MethodInvoker Delegate
If you want to invoke a method with void parameter list?
you can use MethodInvoker Delegate.
E.g
you can use MethodInvoker Delegate.
E.g
private void someMethod() {
myOtherForm.Invoke(new MethodInvoker(delegate() {
label1.Text ="Update Label";
}));
}
Table Variables Vs Temp Table
What is the difference and use of Table Variable vs Temp Table. To know more read
http://odetocode.com/code/365.aspx
http://odetocode.com/code/365.aspx
EnumerateDirectories in .Net 4.0
if you want to return Enumerable collection of file information you can use EnumerateFiles
IEnumerable = DirInfo.EnumerateFiles();
Here is the link for Files,Directories,File system information and Lines from a text file
MsdnLink: http://msdn.microsoft.com/en-us/library/dd997370.aspx
DirectoryInfo DirInfo = new DirectoryInfo(@"\\dir");
IEnumerable
Here is the link for Files,Directories,File system information and Lines from a text file
MsdnLink: http://msdn.microsoft.com/en-us/library/dd997370.aspx
use of Explicit interface implementation
Here is a good post for the use of Explicit interface implementation have a look
SO: http://stackoverflow.com/questions/5896209/solution-for-multiple-inheritance-with-opportunity-to-change-protection-lvl
SO: http://stackoverflow.com/questions/5896209/solution-for-multiple-inheritance-with-opportunity-to-change-protection-lvl
System.Object.GetType vs System.Type.GetType
System.Object.GetType- Gets the Type of the current instance
http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx
System.Type.GetType -Gets a Type object that represents the specified type
http://msdn.microsoft.com/en-us/library/system.type.gettype.aspx
SO Link: http://stackoverflow.com/questions/5895871/difference-between-system-object-gettype-and-system-type-gettype/5895985#5895985
http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx
System.Type.GetType -Gets a Type object that represents the specified type
http://msdn.microsoft.com/en-us/library/system.type.gettype.aspx
SO Link: http://stackoverflow.com/questions/5895871/difference-between-system-object-gettype-and-system-type-gettype/5895985#5895985
Wednesday, May 4, 2011
How to Format date in DataPicker
calanderFrom.Format = DateTimePickerFormat.Short;
calanderFrom.CustomFormat = "dd MMM yyyy";
always give "dd/mm/yyyy
you have to do it incalanderFrom.Format = DateTimePickerFormat.Custom
calanderFrom.CustomFormat = "dd MMM yyyy";
Msdn Linkhttp://msdn.microsoft.com/en-us/library/h2ef6zxz.aspx
http://msdn.microsoft.com/en-us/library/aa236616(v=vs.60).aspx
SO Link
http://stackoverflow.com/questions/5893626/datepicker-is-not-picking-custom-date-format
Subscribe to:
Posts (Atom)