Fixed Keyword

3/31/2004 10:01:39 AM

Fixed Keyword

I have been looking over the technical preview of Whidbey and have found the documentation to be lacking for many of the new features in C#. For example, in Whidbey, it is now possible to have fixed arrays at least inside structs. This was mentioned at the C# talk at PDC. These arrays are stored inline, and, I believe, only work with value types. Since length information is omitted, no range checking is performed; hence, code that uses this feature must be declared unsafe. This feature is mainly provided to support calls to Win32 that utilize inlined arrays, but the CLR has other ways to accomplish this.

	unsafe class Program
	{
		unsafe struct FixedStruct
		{
			public fixed int array[10];
			public int overflow;
		}
 
		static void Main(string[] args)
		{
			FixedStruct fixedStruct = new FixedStruct();
			fixedStruct.array[10] = 2;
			fixedStruct.array[10] = 3;
			Console.WriteLine(fixedStruct.overflow);
			Console.ReadLine();
		}
	}

Comments

 

Navigation

Categories

About

Net Undocumented is a blog about the internals of .NET including Xamarin implementations. Other topics include managed and web languages (C#, C++, Javascript), computer science theory, software engineering and software entrepreneurship.

Social Media