Export (0) Print
Expand All

CodeDefaultValueExpression Class

Represents a reference to a default value.

System.Object
  System.CodeDom.CodeObject
    System.CodeDom.CodeExpression
      System.CodeDom.CodeDefaultValueExpression

Namespace:  System.CodeDom
Assembly:  System (in System.dll)

[SerializableAttribute]
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
public class CodeDefaultValueExpression : CodeExpression

The CodeDefaultValueExpression type exposes the following members.

  NameDescription
Public methodCodeDefaultValueExpression()Initializes a new instance of the CodeDefaultValueExpression class.
Public methodCodeDefaultValueExpression(CodeTypeReference)Initializes a new instance of the CodeDefaultValueExpression class using the specified code type reference.
Top

  NameDescription
Public propertyTypeGets or sets the data type reference for a default value.
Public propertyUserDataGets the user-definable data for the current object. (Inherited from CodeObject.)
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

A CodeDefaultValueExpression can be used to represent a reference to a default value.

The Type property specifies the reference to the value type. The CodeDefaultValueExpression class is used in the generation of generics-based code. For more information on generics, see Generics in the .NET Framework Class Library (C# Programming Guide). The following code steps are provided in this section to further describe the use of the CodeDefaultValueExpression class to add a new default value to a code graph.

The code in part 1 is part of a larger example provided for the CodeTypeParameter class. This code, when run through the C# code generator, results in the C# code that appears in part 2. When this code is called in the statement in part 3, the result is the output shown in part 4.

// Part 1: Code to create a generic Print method.
            CodeMemberMethod printMethod = new CodeMemberMethod();
            CodeTypeParameter sType = new CodeTypeParameter("S");
            sType.HasConstructorConstraint = true;
            CodeTypeParameter tType = new CodeTypeParameter("T");
            sType.HasConstructorConstraint = true;

            printMethod.Name = "Print";
            printMethod.TypeParameters.Add(sType);
            printMethod.TypeParameters.Add(tType);
            printMethod.Statements.Add(ConsoleWriteLineStatement(
                new CodeDefaultValueExpression(new CodeTypeReference("T"))));
            printMethod.Statements.Add(ConsoleWriteLineStatement(
                new CodeDefaultValueExpression(new CodeTypeReference("S"))));

// Part 2: Code generated by code in part 1.
public virtual void Print<S, T>()
            where S : new()
         {
            Console.WriteLine(default(T));
            Console.WriteLine(default(S));
        }

// Part 3: Call to the generated method.
dict.Print<System.Decimal, int>();

// Part 4: Output of the generated method.
0
0

The following code example shows the use of the CodeDefaultValueExpression to create default values for decimal and integer parameters. This example is part of a larger example provided for the CodeTypeParameter class.

printMethod.Statements.Add(ConsoleWriteLineStatement(
    new CodeDefaultValueExpression(new CodeTypeReference("T"))));
printMethod.Statements.Add(ConsoleWriteLineStatement(
    new CodeDefaultValueExpression(new CodeTypeReference("S"))));

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft