/// <summary> /// Returns the text of the [Description("text")] attribute on an enum. Or null if not found. /// </summary> /// <param name="element"></param> /// <returns></returns> [ CanBeNull ] public static String Description( this Enum element ) { var type = element.GetType(); var memberInfo = type.GetMember( element.ToString() ); if ( !memberInfo.Any() ) { return null; } var attributes = memberInfo[ 0 ].GetCustomAttributes( typeof( DescriptionAttribute ), false ); return attributes.Any() ? ( attributes[ 0 ] as DescriptionAttribute )?.Description : null; }
Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts
Friday, July 1, 2016
C# Enum.Description() extension method
Saturday, June 30, 2012
My thoughts on Visual Studio 2012 RC
Visual Studio 2012.. All caps on the menu. REALLY? Why?
await.. really? I just don't understand it yet, I guess.
await.. really? I just don't understand it yet, I guess.
Friday, March 23, 2012
Pick a Random Enum in C# (Better than my old post)
static T RandomEnum() {
if ( typeof( T ).IsEnum ) {
var names = Enum.GetNames( typeof( T ) );
return ( T )Enum.Parse( typeof( T ), names[ threadsafeRandom.Value.Next( 0, names.Length ) ] );
}
return default( T );
}
if ( typeof( T ).IsEnum ) {
var names = Enum.GetNames( typeof( T ) );
return ( T )Enum.Parse( typeof( T ), names[ threadsafeRandom.Value.Next( 0, names.Length ) ] );
}
return default( T );
}
static readonly ThreadLocal threadsafeRandom = new ThreadLocal( () => new Random( RandomSeed() ) );
Tuesday, August 30, 2011
Programmers. Sigh.
Programmers.
Sigh.
You know those pesky 'warnings' that your build pukes up?
Yah... stop ignoring them.
You're most likely doing whatever it is you intended on doing the wrong way.
/sigh
Sigh.
You know those pesky 'warnings' that your build pukes up?
Yah... stop ignoring them.
You're most likely doing whatever it is you intended on doing the wrong way.
/sigh
Thursday, July 21, 2011
Low Orbit Ion Cannon
I just checked out the source code from the LOIC (Wikipedia) project.
Wow.
As in.. "Wow, what immature dumbass wrote this C# code?" !
Horrible design and flaky as crap.
Good thing to know we are being 'freed' by 'people' with these 'elite' 'skillz'.
Ugh, now I feel dirty.. fap fap fap.
Wow.
As in.. "Wow, what immature dumbass wrote this C# code?" !
Horrible design and flaky as crap.
Good thing to know we are being 'freed' by 'people' with these 'elite' 'skillz'.
Ugh, now I feel dirty.. fap fap fap.
Sunday, May 29, 2011
C#: Another HasDuplicates() for IEnumerable
public static bool HasDuplicates_Version2<T>( this IEnumerable<T> sequence ) {
if ( null == sequence ) {
throw new ArgumentNullException( "sequence" );
}
var set = new HashSet<T>();return !sequence.All( set.Add );}
C#: HasDuplicates() extension for IEnumerable
Continuing on my previous post..
public static bool HasDuplicates<T>( this IEnumerable<T> sequence ) {
if ( null == sequence ) {
throw new ArgumentNullException( "sequence" );
}
return sequence.Duplicates().Any();}
C#: IEnumerable get Duplicates (Select Non-Distinct with LINQ)
Saw this page: Raucous Random Ramblings: Select Non-Distinct with Linq!
[Updated 2012-06-12]
Not too shabby.. FWIW: the GetDuplicates() from the raucous random ramblings post returns all items it considers are duplicates.
This is different than my version, which only returns items after they are found in the list. I consider my version better because: if I existed and someone made a clone of me (i.e. 'duplicated'), then I would not consider myself contained in the set of the duplicates. :) But I can understand someone using both functions for different purposes.)
Here is my version for getting duplicates from an enumerable list.
Better? You try both and then let me know what your results are..
In the testing I slimmmed the two foreach down to "return results.SelectMany( @group => @group )" per Resharper's suggestion.
Examples for the set of
Duplicates() returns {6,4,2,4,4,6}
GetDuplicates() returns {2,2,2,4,4,4,6,6,6}
Speed tests!
I threw together a function timer with a massive UInt64[1048576] to check for duplicates.
Letting the tests run for a few minutes (over the same set of data)...
Ignoring the duplicate duplicates that both functions return with a .Distinct() did not make a noticeable timing impact.
Increasing the count of duplicates in the test data increased the speed of both function.
I do like the result set my function returns, and it does perform 3X faster than the raucous post..
[Updated 2012-06-12]
Not too shabby.. FWIW: the GetDuplicates() from the raucous random ramblings post returns all items it considers are duplicates.
This is different than my version, which only returns items after they are found in the list. I consider my version better because: if I existed and someone made a clone of me (i.e. 'duplicated'), then I would not consider myself contained in the set of the duplicates. :) But I can understand someone using both functions for different purposes.)
Here is my version for getting duplicates from an enumerable list.
Better? You try both and then let me know what your results are..
/// <summary>/// Returns duplicate items found in the <see cref = "sequence" />./// </summary>public static IEnumerable<T> Duplicates<T>( this IEnumerable<T> sequence ) {
if ( null == sequence ) {
throw new ArgumentNullException( "sequence" );
}
var set = new HashSet<T>();return sequence.Where( item => !set.Add( item: item ) );}
In the testing I slimmmed the two foreach down to "return results.SelectMany( @group => @group )" per Resharper's suggestion.
Examples for the set of
{1,2,3,4,5,6,7,8,9,6,4,2,2,4,6}I threw together a function timer with a massive UInt64[1048576] to check for duplicates.
Letting the tests run for a few minutes (over the same set of data)...
- Duplicates() processes at 3338 units every millisecond.
- GetDuplicates() processes at 1060 units every millisecond.
Ignoring the duplicate duplicates that both functions return with a .Distinct() did not make a noticeable timing impact.
Increasing the count of duplicates in the test data increased the speed of both function.
I do like the result set my function returns, and it does perform 3X faster than the raucous post..
Saturday, February 5, 2011
Wednesday, December 22, 2010
Enchant Shield - Protection
Enchant Shield - Protection
Really Blizzard?
160 armor on a level 300 enchant?
You seem to have missed some more numbers..
Hire me. I'll fix your code :)
Saturday, August 28, 2010
C# vs C++
Why do some 'programming' sites mix C++ and C# material?
I mean, come on! They're not even close.. :P
Any comments ?
I mean, come on! They're not even close.. :P
Any comments ?
Sunday, June 6, 2010
I love ConcurrentBag.
There is an informative post about the new thread-safe collection over at CodeThinked.
Check it out and be sure to read the comments.
Check it out and be sure to read the comments.
Subscribe to:
Posts
(
Atom
)