ConditionalWeakTable<TKey,TValue>.GetOrCreateValue(TKey) Method

Definition

Atomically searches for a specified key in the table and returns the corresponding value. If the key does not exist in the table, the method invokes the parameterless constructor of the class that represents the table's value to create a value that is bound to the specified key.

public TValue GetOrCreateValue (TKey key);

Parameters

key
TKey

The key to search for. key represents the object to which the property is attached.

Returns

TValue

The value that corresponds to key, if key already exists in the table; otherwise, a new value created by the parameterless constructor of the class defined by the TValue generic type parameter.

Exceptions

key is null.

The class that represents the table's value does not define a parameterless constructor.

Note: In the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, MissingMemberException, instead.

Examples

The following example defines a MainClass class and a MainInfo class, which provides information about the MainClass instance. The example calls the GetOrCreateValue method to add a MainClass object and its attached MainInfo object to a ConditionalWeakTable<TKey,TValue> table. The example also illustrates calls to the Add and GetValue methods to add key/value pairs to the table, and to the TryGetValue method to retrieve the value of an existing key.

using System;
using System.Reflection;
using System.Runtime.CompilerServices;

public class Example
{
   string Name; 
   
   public Example(string name)
   {
      this.Name = name;
   }
   
   public override string ToString()
   {
      return this.Name;
   }
}

// Define a class to contain information about each Example instance.
public class ExampleInfo
{
   public string Name;
   public int Methods;
   public int Properties;
   
   public override string ToString()
   {
      return String.Format("{0}: {1} Methods, {2} Properties", 
                           this.Name, this.Methods, this.Properties);
   }
}

public class ExampleTest
{
   private static BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;

   public static void Main()
   {
      Example ex1 = new Example("ex1");
      Example ex2 = new Example("ex2");
      Example ex3 = new Example("ex3");
      
      ExampleInfo exInfo1 = new ExampleInfo(); 
      exInfo1.Name = ex1.ToString();
      exInfo1.Methods = ex1.GetType().GetMethods(flags).Length;
      exInfo1.Properties = ex1.GetType().GetProperties(flags).Length;
      
      ExampleInfo exInfo3 = new ExampleInfo(); 
      exInfo3.Name = ex3.ToString();
      exInfo3.Methods = ex3.GetType().GetMethods(flags).Length;
      exInfo3.Properties = ex3.GetType().GetProperties(flags).Length;

      var attached = new ConditionalWeakTable<Example, ExampleInfo>();
      ExampleInfo value = null;

      // Attach a property to ex1 using the Add method, then retrieve it.
      attached.Add(ex1, exInfo1);
      if (attached.TryGetValue(ex1, out value))
         Console.WriteLine("{0}, {1}", ex1, value);
      else
         Console.WriteLine("{0} does not have an attached property.", ex1);

      // Attempt to retrieve the value attached to ex2.
      value = attached.GetValue(ex2, ExampleTest.CreateAttachedValue);      
      if (attached.TryGetValue(ex2, out value))
         Console.WriteLine("{0}, {1}", ex2, value);
      else 
         Console.WriteLine("{0} does not have an attached property.", ex2);
      
      // Attempt to retrieve the value attached to ex3.
      value = attached.GetOrCreateValue(ex3);
      Console.WriteLine("{0}, {1}", ex3, value);
   }

   public static ExampleInfo CreateAttachedValue(Example ex)
   {
      ExampleInfo info = new ExampleInfo();
      info.Name = ex.ToString();
      info.Methods = ex.GetType().GetMethods(flags).Length;
      info.Properties = ex.GetType().GetProperties(flags).Length;
      return info;
   }
}
// The example displays the following output:
//       ex1, ex1: 4 Methods, 0 Properties
//       ex2, ex2: 4 Methods, 0 Properties
//       ex3, : 0 Methods, 0 Properties

Remarks

If key does not exist in the table, the method adds it, along with the object that is instantiated by calling the parameterless constructor of the class defined by the TValue generic type parameter. If the TValue class has no parameterless constructor, a MissingMethodException is thrown.

This is the recommended method of retrieving an existing value or adding a new value to the ConditionalWeakTable<TKey,TValue> table if the class of the table's value defines a parameterless constructor. If it does not define a parameterless constructor, you can instead call the GetValue method, which relies on a callback-provided method to instantiate the object representing the table's value

To retrieve the value of an existing key without adding the key/value pair if the key is not found in the table, call the TryGetValue method.

Applies to

Product Versies
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

See also