Standing on the shoulders of giants. RSS 2.0
Previous Page Page 2 of 2 in the DevelopmentCodesnippet category
# Wednesday, August 29, 2007
int[] source = new int[]{1,2,3,4,5,6};

List<int> list = new List<int>( source );

Debug.Assert( ((IList<int>)list).IsReadOnly == false ); // false

Collection<int> collection = new Collection<int>( source );

Debug.Assert(  ((IList<int>)collection).IsReadOnly == true ); // true!!
When the documentation says 'wrapper' they really mean it.

Wednesday, August 29, 2007 10:33:20 AM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] - Trackback
BCL | Codesnippet
# Tuesday, August 28, 2007

After reading post about List Predicates at the very informative BCL Team Blog, I did some research to see how flexible the predicates actually are.

I found out that none of the methods taking a Predicate as a parameter support chained delegates (more info about combining delegates), only the delegate added last to the chain is executed in my experience.

Example:

 

// Prepare collection
List<int> list = new List<int>( new int[]{1,2,3,4,5,6} );
        
// function to determine if the int is less than 5
Predicate<int> LT5 = delegate(int x){ return x < 5; };
        
// function to determine if the int is greater than 2
Predicate<int> GT2 = delegate(int x){ return x > 2; };
        
// combine the predicates:
// return true for 2 < x < 5
Predicate<int> GT2LT5 = null;
GT2LT5 += GT2;
GT2LT5 += LT5;
        
List<int> result = list.FindAll(GT2LT5);

// writes 1,2,3,4
result.ForEach( delegate( int x) { Console.WriteLine( x ); });

A work around would be to use the second delegate on the result collection after calling the FindAll method with the first delegate, but this doesn't scale with more delegates.

 

// first use the first delegate
List<int> intermediate = list.FindAll(LT5);
// now use the second delegate on the intermediate result
List<int> result = intermediate.FindAll(GT2);

// writes 3,4
result.ForEach( delegate( int x) {
    Console.WriteLine( x );
});
 

Another solution is to enumerate through the collection and call each delegate in the invocation list.

 

// Prepare collection
List<int> list = new List<int>( new int[]{1,2,3,4,5,6} );

// function to determine if the int is less than 5
Predicate<int> LT5 = delegate(int x){
return x < 5;
};

// function to determine if the int is greater than 2
Predicate<int> GT2 = delegate(int x){
return x > 2;
};
        
// combine the predicates:
// return true for 2 < x < 5
Predicate<int> GT2LT5 = null;
GT2LT5 += GT2;
GT2LT5 += LT5;

// prepare result
List<int> result = new List<int>();

foreach(int i in list ){
    
    bool valid = true;
    
    // GetInvocationList returns all combined delegates
    foreach( Predicate<int> match in GT2LT5.GetInvocationList() ){
        valid = valid & match(i);            
    }

    if( valid ){
        result.Add(i);
    }
}

// writes 3,4
result.ForEach( delegate( int x) {
    Console.WriteLine( x );
});

Both solutions are less than ideal.

Tuesday, August 28, 2007 10:26:33 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] - Trackback
Codesnippet | BCL
# Tuesday, June 26, 2007

The week number depends on 4 variables:

  1. The date you're interested in,
  2. The calendar you use (e.g. Gregorian, Julian, Hebrew etc.)
  3. The first day of the week
  4. The week you count as 1 (e.g. the week containing Jan 1, the first 4 day week etc.)

 

All these parameters are included in the Calendar.GetWeekOfYear method, as shown in the following code snippet:

// instantiate the calendar used in the Netherlands
Calendar calendar = new GregorianCalendar();

// get the current week for the Netherlands
calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);


// and for the USA
calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
Tuesday, June 26, 2007 3:31:55 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] - Trackback
Codesnippet
# Friday, August 12, 2005

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

Note: this will be 0.0.0.0 if you use it in the code-forward of an aspx page.

Friday, August 12, 2005 11:54:58 AM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] - Trackback
ASP.NET | Codesnippet
Ads
About
© Copyright 2010
Paul van Brenk
Sign In
newtelligence dasBlog 2.3.9074.18820
All Content © 2010, Paul van Brenk
DasBlog theme 'Business' created by Christoph De Baene (delarou)