I don't like to using Struct just for Performance reason. Because it made it easier to has bug in your application.
And it is worse in Team environment.  If an application has a stuct employeeDTO, because this DTO only hold Employee values and qualify to be a value type .
In one place the code got an EmployeeDTO and stored it to a collection(for example, session or ArrayList).

See code like this
ArrayList EmpList=new ArrayList();
EmpList.add(aEmpDto);

....

EmployeeDTO empDto=(EmpleoyeeDTO)EmpList[0];
empDto.email="newemail@test.com"

or
EmployeeDTO empDto=(EmpleoyeeDTO)Session["MyEmpDTO"];
empDto.email="newemail@test.com"

UnBox happen here. Many people will forget that the employeeDTO is value type and treat it as reference type. They change the value and didn't save it back to session.


if in another page, he want to display this updated email address.
Response.write(((EmpleoyeeDTO)session["myEmployeeDto"]).email)
It will still print the old email address.

you may say a good programmer with good programming habit will not cause that bug. But you can't relay on all the members in your team will never make mistake like this.
So I always avoid to create new value type in my code. I may lost some performance, but prevent some potential bugs.


 
Categories: C#


1. you need create a web deploy project, if you didn't have that,please install the web deployment plug-in.

For Visual Studio 2005, the plug-in can be downloaded here.

For Visual Studio 2008, the plug-in can be downloaded here.


2. Configure your deploy project properties like below.





3. after build, you will get a single assembly CommonVoiceUC.dll,
in you new web site, add it to your reference.




4. in your aspx page, use it like regular Customer Controls.
<%@ Register Assembly="CommonVoiceUC" Namespace="CVUC" TagPrefix="cvuc" %>
 
<cvuc:CvDistributionGroupUpdateUC runat="server" ID="test" />


Conclusion:
so this way you can very easy create a lot of customer controls with lots of Html tag or css in it. and it very easy for other application to use it.
the bad thing is if the control have other components, for example "Telerik.Web.UI.dll", and other site want to reuse this control was using a different version of Telerik control.
the conflicts will happen.

 
Categories: Asp.net | VS 2005 | VS 2008

Here is a example of using yield:

public static IEnumerable<int> Unique(IEnumerable<int> nums)
{
Dictionary<int, int> uniqueVals = new
Dictionary<int, int>();
foreach (int num in nums)
{
if (!uniqueVals.ContainsKey(num))
{
uniqueVals.Add(num, num);
yield return num;
}
}
}


Dompare the typical way and the "yield" to get IEnumerable

# IList<string> FindBobs(IEnumerable<string> names)  
# {
# var bobs = new List<string>();
#
# foreach(var currName in names)
# {
# if(currName == "Bob")
# bobs.Add(currName);
# }
#
# return bobs;
# }
# IEnumerable<string> FindBobs(IEnumerable<string> names)  
# {
# foreach(var currName in names)
# {
# if(currName == "Bob")
# yield return currName;
# }
# }



 
Categories: C# | LINQ and C# 3.0