Posts

Showing posts from February, 2010

Enumerable Method Series : Enumerable.Aggregate

int[] a = { 1, 2, 4, 5, 6, 7, 8, 9, 10 }; Method #1 int b = a.Aggregate((x, y) => x += y); // Simple Aggregate (Result : 52) //Alternative or Interpretation // for (int i = 0; i <= a.GetUpperBound(0); i++) x += a[i]; int b = x; Method #2 int c = a.Aggregate(0, (x, y) => x += y); // Initialize with value(0) before aggregate process start (Result : 52) //Alternative or Interpretation // int x = 0; for (int i = 0; i <= a.GetUpperBound(0); i++) x += a[i]; int c = x; Method #3 int d = a.Aggregate(0, (x, y) => x += y, z => z >= 1 ? z : 0); // Initialize with value(0) before aggregate process start // After aggregate process done final result will move to // another section for further process on final result. // Like here if result is greater than 0, it will return aggregated result else 0 //Alternative or Interpretation // int x = 0; for (int i = 0; i <= a.GetUpperBound(0); i++)

Difference between Parse/TryParse/ParseExact/TryParseExact

Parse ===== //int.Parse("12.44"); //Exception : Above code will generate the exception "Input string was not in a correct format", just because you are trying to cast float/real value in int[Integer]. //Only it is parsing if your parameter value must match the desire type in which you are trying to parse. TryParse ======== //case #1 int.TryParse("14.55",out a); //case #2 int.TryParse("14", out a); //Above code(case 1) will return 'false', just because parsing operation gets failed and value for variable 'a' would be 'zero' //here consider 'a' is output type of parameter like we have in oracle/sql server(not exactly same but behavior is same). //if parsing get succeeded than it will return true and target variable (i.e. 'a') will have the

How to check session timeout before accessing session item?

Here, I am trying to highlight "object reference not set" issue due to session time out or if session timed out than decide your next operation keeping in mind that reader know how session maintain in ASP.NET Purpose : To avoid object "Object reference not set" exception that comes when trying to access session element after timeout.   Prescribed Solution : It's hard to check session existence in every step. So, what we can do is just check the session existence with the help of session id and one property (IsNewSession: This property will return true if new session initiated). We can implement this check either on ASPX page level or master page. protected void Page_Init(object sender, EventArgs e) { if (Context.Session!=null) { if (Session.IsNewSession) { string SessionHdr =String.Empty; if (Session.IsCookieless) SessionHdr = Request.Headers["AspFilterSessionId"]; else { SessionHdr = Request.Headers["Cookie"];