cref Attribute (C# Programming Guide)

The cref attribute in an XML documentation tag means "code reference." It specifies that the inner text of the tag is a code element, such as a type, method, or property. Documentation tools like Sandcastle use the cref attributes to automatically generate hyperlinks to the page where the type or member is documented.

Example

The following example shows a cref attribute used in a <see> tag.

// compile with: /doc:DocFileName.xml  

/// text for class TestClass 
public class TestClass
{
    /// <summary> 
    /// The GetZero method. 
    /// </summary> 
    /// <example> This sample shows how to call the <see cref="TestClass.GetZero"/> method.
    /// <code> 
    /// class TestClass  
    /// { 
    ///     static int Main()  
    ///     { 
    ///         return GetZero(); 
    ///     } 
    /// } 
    /// </code> 
    /// </example> 
    public static int GetZero()
    {
        return 0;
    }

    /// text for Main 
    static void Main()
    {
    }
}

When compiled, the program produces the following XML file. Note that the cref attribute has been transformed by the compiler to "M:TestBedForWrap3.Wrap3.TestClass.GetZero". The "M:" prefix means "method" and is a convention that is recognized by documentation tools such as Sandcastle. For a complete list of prefixes, see Processing the XML File (C# Programming Guide).

<?xml version="1.0"?>
<doc>
    <assembly>
        <name>TestBedForWrap3</name>
    </assembly>
    <members>
        <member name="T:TestBedForWrap3.Wrap3.TestClass">
            text for class TestClass
        </member>
        <member name="M:TestBedForWrap3.Wrap3.TestClass.GetZero">
            <summary>
            The GetZero method.
            </summary>
            <example> This sample shows how to call the <see cref="M:TestBedForWrap3.Wrap3.TestClass.GetZero"/> method.
            <code>
            class TestClass 
            {
                static int Main() 
                {
                    return GetZero();
                }
            }
            </code>
            </example>
        </member>
        <member name="M:TestBedForWrap3.Wrap3.TestClass.Main">
            text for Main
        </member>
    </members>
</doc>

See Also

Reference

XML Documentation Comments (C# Programming Guide)

Recommended Tags for Documentation Comments (C# Programming Guide)

Change History

Date

History

Reason

September 2008

Added topic.

Customer feedback.