Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Development Edition
 Anonymous Methods and Code Analysis
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Visual Studio Team System
Anonymous Methods and Code Analysis

An anonymous method is just a method that has no name. Anonymous methods are most frequently used to pass a code block as a delegate parameter.

This topic explains how Code Analysis handles warnings and metrics that are associated with anonymous methods.

Warnings and metrics for an anonymous method that is declared in a member, such as a method or accessor, are associated with the member that declares the method. They are not associated with the member that calls the method.

Description

For example, in the following class, any warnings found in the declaration of anonymousMethod, should be raised against Method1 and not Method2.

Visual Basic
Imports System

Delegate Function ADelegate(ByVal value As Integer) As Boolean

Class AClass

    Sub Method1()
        Dim anonymousMethod As ADelegate = Function(ByVal value As Integer) value > 5
        Method2(anonymousMethod)
    End Sub

    Sub Method2(ByVal anonymousMethod As ADelegate)
        anonymousMethod(10)
    End Sub

End Class
C#
using System;

delegate void Delegate();

class Class1
{
    void Method1()
    {
        Delegate anonymousMethod = delegate()
        {
            Console.WriteLine("");
        };
        Method2(anonymousMethod);
    }

    void Method2(Delegate anonymousMethod)
    {
        anonymousMethod();
    }
}

Warnings and metrics for an anonymous method declared as an inline assignment to a field are associated with the constructor. If the field is declared as static (Shared in Visual Basic), then the warnings and metrics are associated with the class constructor; otherwise, they are associated with the instance constructor.

Description

For example, in the following class, any warnings found in the declaration of anonymousMethod1, will be raised against the implicitly generated default constructor of Class. Whereas, those found within anonymousMethod2, will be applied against the implicitly generated class constructor.

Visual Basic
Imports System

Delegate Function ADelegate(ByVal value As Integer) As Boolean

Class AClass
    Dim anonymousMethod1 As ADelegate = Function(ByVal value As Integer) value > 5
    Shared anonymousMethod2 As ADelegate = Function(ByVal value As Integer) value > 5
    Sub Method1()
            anonymousMethod1(10)
            anonymousMethod2(10)
        End Sub
End Class
C#
using System;

namespace Sample
{
    delegate void Delegate();
    class Class2
    {
        Delegate anonymousMethod1 = delegate()
        {
            Console.WriteLine("");
        };

        static Delegate anonymousMethod2 = delegate()
        {
            Console.WriteLine("");
        };

        void Method()
        {
            anonymousMethod1();
            anonymousMethod2();
        }
    }
}

A class could contain an inline anonymous method that assigns a value to a field that has multiple constructors. In this case, warnings and metrics are associated with all the constructors unless that constructor chains to another constructor in the same class.

Description

For example, in the following class, any warnings found in the declaration of anonymousMethod, should be raised against Class(int) and Class(string), but not against Class().

Visual Basic
Imports System

Delegate Function ADelegate(ByVal value As Integer) As Boolean
Class AClass

    Dim anonymousMethod As ADelegate = Function(ByVal value As Integer) value > 5

    Sub New()
    End Sub

    Sub New(ByVal a As Integer)
    End Sub

    Sub New(ByVal a As String)
    End Sub

End Class
C#
using System;

delegate void Delegate();

class Class
{
    Delegate anonymousMethod = delegate()
    {
        Console.WriteLine("");
    };
    Class()
        : this((string)null)
    {
    }

    Class(int a)
    {
    }

    Class(string a)
    {
    }
}

Even though this might seem unexpected, this occurs because the compiler outputs a unique method for every constructor that does not chain to another constructor. Because of this behavior, any violation that occurs within anonymousMethod, must be suppressed separately. This also means that if a new constructor is introduced, warnings that were previously suppressed against Class(int) and Class(string) must also be suppressed against the new constructor.

You can work around this issue in one of two ways. You could declare anonymousMethod in a common constructor that all constructors chain. Or you could declare it in an initialization method that is called by all constructors.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker