P/Invoke Cheat Sheet  

Posted by Editor In Chief

Useful cheat sheet from Tamir Khason.

Great multithreading article  

Posted by Editor In Chief

A comprehensive and accessible tutorial on multithreading in C# by Joe Albahari, author of LinqPad. Gradually introduces the topic leading up to advanced topics such as memory barriers and thread local storage.

SIlverlight: How to determine a control's left and top property  

Posted by Editor In Chief

When your control is NOT contained in a canvas it can be tricky determining it's top and left coordinates.

It turns out however that every UIElement derived control (which is virtually all controls with some visual interface) has a TransformToVisual method which allows you to acquire a transform object that can translate points relative to the controls coordinates. This allows it to transform the point (0,0) to the control's left and top coordinate values.


...
GeneralTransform generalTransform = this.TransformToVisual(null);
Point myLeftTop = generalTransform.Transform(new Point(0,0));
...


:)

Silverlight: Hit Testing on Shapes  

Posted by Editor In Chief

Whilst working on a small project which required drag and drop functionality I stumbled when a templated control which I had programmed to accept drops refused to show up as a hit test item. This particular control used a rectangle to provide it's visuals.

What I had to remember was that the surface area of a shape consists of it's outline(stroke) and it's fill (if a fill is specified). My control's rectangle had an outline but did not require a fill.


<Rectangle
Grid.Row="1" Grid.Column="0"
Stroke="Pink"
Height="50"
Width="50"
StrokeThickness="5"
MouseLeftButtonUp="Rectangle_MouseLeftButtonUp"/>


The event handler MouseLeftButtonUp is defined as below:

private void Rectangle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
HtmlPage.Window.Alert("Hello from Mr Rectangle");
}


With the current definition of the rectangle as it is the mouse event will only fire if you click on the shape's outline. Nothing happens if you click inside the shape.

Adding a transparent fill brings the inside of the shape into play from a hit testing perspective.


<Rectangle
...
Fill="Transparent"
... />

LINQ: Using SelectMany to flatten lists of lists.  

Posted by Editor In Chief in ,

When you run a projection via a method which returns an IEnumerable<T> the result of your projection ends up being an IEnumberable<IEnumerable<T>>. In these cases you might actually prefer a flattened IEnumerable. Well, the SelectMany method does just that.

Eg, say you want an IEnumerable of the usages of a custom attribute in an assembly.


class AttributesReader
{
public static IEnumerable<MyAttributeInfo> RetrieveBAUManagedItems(Assembly assembly)
{
var typesInAssembly = assembly.GetTypes();
if (typesInAssembly.Length > 0) {
var attributesInTypes = typesInAssembly.SelectMany(type => type.GetCustomAttributes(typeof(MyCustomeAttribute), true));
...
}
}


On the other hand simply using
typesInAssembly.Select(type => type.GetCustomAttributes(typeof(MyCustomeAttribute), true))
would return an IEnumerable<object[]>.

CodeRush Xpress for C#  

Posted by Editor In Chief in

http://msdn.microsoft.com/en-us/vcsharp/dd218053.aspx

LINQ: Find duplicates  

Posted by Editor In Chief in ,


var nums = new[] { 1,2,2,4,5,5,6,7,8,8,8,9 };
var distinct = nums.Distinct();

if(nums.Count() != distinct.Count())
{
nums.GroupBy(n => new { DuplicatedNumber = n })
.Where(g => g.Count() > 1)
.Select(g => new { number = g.Key.DuplicatedNumber, numberOfDuplicates = g.Count() }).Dump("Duplicates found");
}
else
{
"No duplicates found".Dump();
}


ps: Get yourself a copy of the excellent free LINQPad and enjoy the wonders of the BIG Dump()!