P/Invoke Cheat Sheet
Posted by Editor In Chief
Useful cheat sheet from Tamir Khason.
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.
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));
...
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"/>
private void Rectangle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
HtmlPage.Window.Alert("Hello from Mr Rectangle");
}
<Rectangle
...
Fill="Transparent"
... />
Posted by Editor In Chief in C#, LINQ
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));
...
}
}
typesInAssembly.Select(type => type.GetCustomAttributes(typeof(MyCustomeAttribute), true))would return an IEnumerable<object[]>.
Posted by Editor In Chief in C#
http://msdn.microsoft.com/en-us/vcsharp/dd218053.aspx
Posted by Editor In Chief in C#, LINQ
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();
}