TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Fixed Statement

This C# program uses the fixed statement in an unsafe code block.

Fixed. The fixed statement fixes memory in one location.

Objects in memory are moved at almost any time. This makes garbage collection possible. But when we use unsafe pointers to memory addresses, that memory must not be moved.

Example. First, this program must be compiled with unsafe code allowed. You can set this option in Visual Studio. It introduces the Transform() method, which internally acquires a random file name string object.

Path.GetRandomFileNameNew String

Then: We use the fixed statement to ensure that the garbage collector does not relocate the string data.

C# program that uses fixed statement on string

using System;

class Program
{
    static void Main()
    {
	Console.WriteLine(Transform());
	Console.WriteLine(Transform());
	Console.WriteLine(Transform());
    }

    unsafe static string Transform()
    {
	// Get random string.
	string value = System.IO.Path.GetRandomFileName();

	// Use fixed statement on a char pointer.
	// ... The pointer now points to memory that won't be moved!
	fixed (char* pointer = value)
	{
	    // Add one to each of the characters.
	    for (int i = 0; pointer[i] != '\0'; ++i)
	    {
		pointer[i]++;
	    }
	    // Return the mutated string.
	    return new string(pointer);
	}
    }
}

Output

61c4eu6h/zt1
ctqqu62e/r2v
gb{kvhn6/xwq

In this program, please look at the fixed statement. Inside this statement is a block of code—it has a guarantee that the string data pointed to by the char* pointer will not be moved.

And: This means we can manipulate that memory however we like. In the example, we increment the value of each character.

Increment Int

Performance. The fixed statement does generate certain intermediate language instructions when used, which impact performance. Often, for this reason, unsafe code and pointer manipulation is slower than managed C# code.

So: This indicates that, in the .NET Framework, it is typically better to use exclusively managed code.

Also: Older languages that do not rely on the .NET Framework will benefit more from pointer manipulation optimizations.

Optimizing with the fixed statement and pointer manipulation is often trial-and-error. Certain classical optimizations will not benefit C# programs and will instead make them slower due to the transition from movable to fixed memory.

Thus: If you are optimizing, make sure to benchmark all the unsafe changes you make.

Benchmark

Summary. We explored the fixed statement in the C# language. The fixed statement is used with array elements, integer fields, and any data that is classified as movable—meaning it could be moved in memory by the garbage collector.

However: The fixed statement imposes a performance penalty, which makes its use as an optimization more complex.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf