次の方法で共有


StyleCop Compliant Visual Studio 2010 Code Snippets November Release

Visual Studio Snippets is an open source collection of code snippets for the C# programming language, providing StyleCop compliant versions of the 38 code snippets that ship within Visual Studio along with 40 additional StyleCop compliant snippets. You can download an Excel spreadsheet containing details of all 78 code snippets here.

As mentioned in my previous blog post about the Visual Studio Snippets collection, you can immediately see the difference if you take a moment to consider the svm.snippet that creates the following code out of the box:

static void Main(string[] args)
{

}

StyleCop rule 1600 requires that the method must have a documentation header and StyleCop rule SA1400 requires that the method must have an access modifier. The Visual Studio Snippets version has been refactored to resolve these two issues.

/// <summary>
/// Defines the program entry point.
/// </summary>
/// <param name="args">An array of <see cref="T:System.String"/> containing command line parameters.</param>
private static void Main(string[] args)
{

Visual Studio Snippets provides refactored versions of all 38 code snippets that ship with Visual Studio and hopefully the example with the svm.snippet above illustrates the benefit of using these refactored code snippets.

Within the November release there are 10 new code snippets, with most of the new snippets focused upon parallel programming with all the static members of the System.Threading.Interlocked class now represented by simple code snippets.

add.snippet allows us to easily call the Interlocked.Add overloaded methods to generate code such as the following:

Interlocked.Add(ref location, value);

cex.snippet allows us to easily call the Interlocked.CompareExchange overloaded methods to generate code such as the following:

Interlocked.CompareExchange(ref location, value, comparand);

cexg.snippet allows us to easily call the generic Interlocked.CompareExchange<T> method to generate code such as the following:

Interlocked.CompareExchange<string>(ref location, value, comparand);

dec.snippet allows us to easily call the Interlocked.Decrement overloaded methods to generate code such as the following:

Interlocked.Decrement(ref value);

ex.snippet allows us to easily call the Interlocked.Exchange overloaded methods to generate code such as the following:

Interlocked.Exchange(ref location, value);

exg.snippet allows us to easily call the generic Interlocked.Exchange<T> method to generate code such as the following:

Interlocked.Exchange<string>(ref location, value);

inc.snippet allows us to easily call the Interlocked.Increment overloaded methods to generate code such as the following:

Interlocked.Increment(ref value);

read.snippet allows us to easily call the Interlocked.Read method to generate code such as the following:

Interlocked.Read(ref location);

Other code snippets within the Visual Studio Snippets collection support parallel programming, such as the lock.snippet that resolves the anti-pattern of locking on this by instead locking on a private field with in the class. In fact there is also a sync.snippet that creates the following code for a private field object that can then be used for thread synchronization and the lock.snippet defaults to locking on the private field created by the sync.snippet.

StyleCop also requires documentation headers for fields and as such the sync.snippet complies with this requirement generating the following code:

/// <summary>
/// Defines a private thread synchronization object for the <see cref="T:Singleton"/> class.
/// </summary>
private object synchronizationObject = new object();

lock.snippet then allows us to lock upon the above synchronization object and therefore creates the following code:

lock (this.synchronizationObject)
{

Coarse grained parallelism can be embraced using the thread.snippet which uses a lambda function to create and start a thread.

Thread thread = new Thread(() =>
{

});

thread.Start();

Fine grained parallelism can also be embraced, this time using the task.snippet.

Task.Factory.StartNew(() =>
{

}); 

Within the November release there are also new code snippets that allow you to implement types that implement the IIdentity and IPrincipal interfaces, download and install the collection and take a look at identity.snippet and principal.snippet.

As I release new versions of the Visual Studio Snippets and Visual Studio Templates projects I would encourage you to give feedback on them or even just let me know that you are using them and hopefully they are making you or your team more productive. If you’re on Twitter you can also contact me at @dougholland and use the #vssnippets or #vstemplates tags.