segunda-feira, 12 de março de 2012

With statement for C# through extension method

Extension methods are really cool feature. I was missing the VB with statement recently, so I thought it will be cool to implement a really simple extension method to do it. Below are a VB.Net, C#, short C# and C# through With extension method: VB.Net
With dataGridView.Columns["customer"]
    .Width = 200
    .HeaderText = "Customer"
    .HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
End With
C#
dataGridView.Columns["customer"].Width = 200;
dataGridView.Columns["customer"].HeaderText = "Customer";
dataGridView.Columns["customer"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
C# short
var column = dataGridView.Columns["customer"];
column.Width = 200;
column.HeaderText = "Customer";
column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
C# With through extension method
dataGridView.Columns["customer"].With(column => {
    column.Width = 200;
    column.HeaderText = "Customer";
    column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
});

static public class ObjectExtensions
{
    static public void With<T>(this T o, Action<T> action)
    {
        action(o);
    }
}

sexta-feira, 7 de outubro de 2011

Simple Parallel ForEach in C#.NET 2.0

A simple Parallel ForEach implementation for Microsoft C#.NET 2.0:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ParallelTest
{
    public class Parallel
    {
        public delegate void Process();

        public static void ForEach<T>(IEnumerable<T> enumerable, Action<T> action) 
        {
            IEnumerator<T> enumerator = enumerable.GetEnumerator();

            int threadCount = Environment.ProcessorCount;

            Process process = delegate()
            {
                while (true)
                {
                    T item = default(T);

                    lock (enumerator)
                    {
                        if (enumerator.MoveNext())
                            item = enumerator.Current;
                        else
                            return;
                    }

                    action(item);
                }
            };

            IAsyncResult[] asyncResults = new IAsyncResult[threadCount];

            for (int i = 0; i < threadCount; ++i)
                asyncResults[i] = process.BeginInvoke(null, null);

            for (int i = 0; i < threadCount; ++i)
                process.EndInvoke(asyncResults[i]);
        }
    }
}

segunda-feira, 3 de outubro de 2011

How to post code?

Follow the example below:


<pre class="brush: html"><h1>
html code here...</h1>
</pre>


Should render like

html code here...

List of available languages:


  • cpp
  • csharp
  • css
  • html
  • java
  • jscript
  • php
  • python
  • ruby
  • sql
  • vb
  • xml
  • perl

syntax highlighting tutorial

Syntax coloring test

public class Syntax
{
    abstract Language Language { get; set; }
}

Source code:

<pre class="brush: csharp">
public class Syntax
{
    abstract Language Language { get; set; }
}
</pre>

segunda-feira, 21 de fevereiro de 2011

First post

Just start typing. My goal here is to collect pieces of artistic code (a.k.a workaround).