MemoryCache Constructor (String, NameValueCollection)

.NET Framework (current version)

Initializes a new instance of the MemoryCache class.

Namespace:   System.Runtime.Caching
Assembly:  System.Runtime.Caching (in System.Runtime.Caching.dll)

public MemoryCache(
	string name,
	NameValueCollection config = null


Type: System.String

The name to use to look up configuration information.

Note   It is not required for configuration information to exist for every name.

If a matching configuration entry exists, the configuration information is used to configure the MemoryCache instance. If a matching configuration entry does not exist, the name can be accessed through the Name property, because the specified name is associated with the MemoryCache instance. For information about memory cache configuration, see MemoryCacheElement.

Type: System.Collections.Specialized.NameValueCollection

A collection of name/value pairs of configuration information to use for configuring the cache.

Exception Condition

name is null.


name is an empty string.


The string value "default" (case insensitive) is assigned to name. The value "default" cannot be assigned to a new MemoryCache instance, because the value is reserved for use by the Default property.


A value in the config collection is invalid.


A name or value in the config parameter could not be parsed.

When the MemoryCache class is initialized, it checks for configuration entries that might have been overridden by using the optional config parameter in the constructor. You can pass the following parameters in the config parameter. All values can be passed as integers.

When this constructor is invoked, configuration settings are first retrieved from application configuration files. If no configuration entries exist in the application configuration file, only the settings provided in config are applied. If the configuration entries in the application configuration exist, and if information is also passed in config, the information in the config overrides the information that is read from the configuration file.

The name value that is assigned to a cache instance is used in two ways:

  • To help you keep track of multiple cache instances when more than one instance exists.

  • To reference settings in the configuration file when the MemoryCache instance is initialized.


    There is no mechanism to enforce unique names for cache instances. Therefore, it is possible to have multiple cache instances with the same name.


    Do not create MemoryCache instances unless it is required. If you create cache instances in client and Web applications, the MemoryCache instances should be created early in the application life cycle. You must create only the number of cache instances that will be used in your application, and store references to the cache instances in variables that can be accessed globally. For example, in ASP.NET applications, you can store the references in application state. If you create only a single cache instance in your application, use the default cache and get a reference to it from the Default property when you need to access the cache.

.NET Framework
Available since 4.0
Return to top