StringFreezingAttribute Class
Collapse the table of content
Expand the table of content

StringFreezingAttribute Class

Note: This class is new in the .NET Framework version 2.0.

Freezes a string literal when creating native images using the Native Image Generator (Ngen.exe). This class cannot be inherited.

Namespace: System.Runtime.CompilerServices
Assembly: mscorlib (in mscorlib.dll)

[AttributeUsageAttribute(AttributeTargets.Assembly, Inherited=false)] 
public sealed class StringFreezingAttribute : Attribute
/** @attribute SerializableAttribute() */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Assembly, Inherited=false) */ 
public final class StringFreezingAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Assembly, Inherited=false) 
public final class StringFreezingAttribute extends Attribute

A frozen string is a string literal object that is compatible with the managed heap and has been serialized into a native image by the Native Image Generator (Ngen.exe). The StringFreezingAttribute attribute allows you to optimize your code by pre-allocating string literals during native image generation rather than at run time. After a string literal has been frozen, code can simply refer to the string using the address in the native image.

The StringFreezingAttribute must be applied on the assembly level.

Note that the common language runtime (CLR) cannot unload any native image that has a frozen string because any object in the heap might refer to the frozen string. Therefore, you should use the StringFreezingAttribute class only in cases where the native image that contains the frozen string is shared heavily.

The following code example demonstrates how to apply the StringFreezingAttribute to an assembly.

using System;
using System.Runtime.CompilerServices;

[assembly :StringFreezingAttribute()]

class Program
    string frozenString = "This is a frozen string after Ngen is run.";
    static void Main(string[] args)

        Console.WriteLine("The StringFreezingAttribute attribute was applied.");



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

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0

Community Additions

© 2015 Microsoft