May
15th

How to Convert List(T1) to List(T2)

Posted by Shahar A |
Filed under .Net, C# |

Did you ever need to convert List(T1) to List(T2)? One example might be when implementing an interface. you might need to expose a collection of other interfaces (or maybe the same interface), But you usually hold the concrete type implementing the interface in the collection. Lets look at the following example:


Continue Reading...
May
13th

Beware of List.Find()

Posted by Shahar A |
Filed under .Net, C# |

List<T>.Find() returns the first element found that matches a given criteria. So lets say we have List<int> and we use the Find method on it. What will be the returned value? If a number in the list matches the criteria it will be returned, but what if not? what does the variable zero contains after executing this code:

List<int> listOfInts = new List<int>(new int[] { 1,2,3,4,5,6,7,8,9});
int zero = listOfInts.Find(
    delegate(int i)
    {
        return i == 0;
    });

It has the value of…


Continue Reading...
May
7th

Why Should You Wrap Your ASP.NET Session Object

Posted by Shahar A |
Filed under .Net, ASP.Net, C# |

ASP.NET provides mechanisms for storing information for a single user session or across multiple sessions. This is done using the HttpSessionState and HttpApplicationState classes. The Page class has Application and Session attributes to provide access to current objects. The simple way to access them is as following:

if (Session["FirstName"] == null)
{
    LabelFirstName.Text = “FirstName”;
}
else
{
    LabelFirstName.Text = (string)Session["FirstName"];
}
if (Session["LastName"] == null)
{
    LabelLastName.Text = “LastName”;
}
else
{
    LabelLastName.Text = (string)Session["LastName"];
}

Continue Reading...
Apr
30th

Call ASP.Net WebMethod from jQuery

Posted by Shahar A |
Filed under ASP.Net, Web Development |

I’ve read about jQuery in this great post Five recommendations for starting a startup with ASP.NET and wanted to play with it and see
how easy it is to call a WebMethod from
sQuery. I Started by reading some of the great Tutorials and downloaded the code from here.
I Created a test web project, copied the jQuery [...]


Continue Reading...
Apr
25th

LINQ to XML in 3 easy steps

Posted by Shahar A |
Filed under .Net, C# |

Linq to xml provides an easy query interface for XML files. In the following example I will demonstrate how to use it for reading and writing data from/to xml file, using the file for persistency maintaining a list of objects. This can be used for storing application settings, storing persistent objects or any other data [...]


Continue Reading...
Apr
22nd

Is [Serializable] == ISerializable? No!

Posted by Shahar A |
Filed under .Net, C# |

Serialization is the process of saving an object onto a storage medium (such as a file, or a memory buffer) or transmiting it across a network connection link in binary form.
The .NET Framework provides an easy-to-use serialization mechanism by using the [Serializable] attribute. If so why do we need the ISerializable interface? Lets compare to see [...]


Continue Reading...
Apr
16th

How to use Aggregate functions with LINQ to SQL

Posted by Shahar A |
Filed under C#, SQL |

LINQ to SQL maps the relational database into an object model, and when the program runs the queries in the object model are translated into SQL. SQL contains Aggregate functions like SUM, AVG, MIN, MAX and more. Lets see how this functionality can be implemented using LINQ Enumerable standard aggregation operators. You can also create [...]


Continue Reading...
Apr
10th

How to Expose Your Collections Safely

Posted by Shahar A |
Filed under .Net, C# |

We use collections all the time. Many times we have to expose them to users of our classes. Lets look at this simple tree node class:
class TreeNode
{
private List<TreeNode> children = new List<TreeNode>();

public IList<TreeNode> Children
{
[...]


Continue Reading...
Apr
7th

Looking for a new domain name?

Posted by Shahar A |
Filed under Technology & Internet |

Finding a good and free domain is not an easy task, and might be a real time consuming. To make it much more efficient and fun, why don’t you do it the web 2.0 way? check out ajaxwhois.com. you can select the preferred extensions and just type in the name you want to check without [...]


Continue Reading...
Apr
3rd

Is WPF Data Binding thread safe?

Posted by Shahar A |
Filed under WPF |

We wanted to check whether WPF data binding is thread safe, and made a little test (using .NET 3.5). Lets look at the Worker class, which has one property - Money, and is making money in a multithreaded way :) - creating 100 threads and increasing the “Money” property from each one of them:
class Worker : INotifyPropertyChanged [...]


Continue Reading...