C# Array IsFixedSize, IsReadOnly and IsSynchronized

Array type

You see that the arrays in the C# programming language have several properties that help you detect features, including IsFixedSize, IsReadOnly and IsSynchronized. What do these properties do and why are they there? This question is answered by reviewing the framework and also through this example program.

Example

In the following block, we see three parts: a C# program that demonstrates how you can use the three properties; next, the output of the program; and finally, the implementation of the properties in the .NET Framework.

Program that uses IsFixedSize, IsReadOnly, IsSynchronized [C#]

using System;

class Program
{
    static void Main()
    {
	int[] array = new int[4];
	Console.WriteLine(array.IsFixedSize);
	Console.WriteLine(array.IsReadOnly);
	Console.WriteLine(array.IsSynchronized);
    }
}

Output

True
False
False

Implementation of properties [.NET Framework]

public bool IsFixedSize
{
    get
    {
	return true;
    }
}

public bool IsReadOnly
{
    get
    {
	return false;
    }
}

public bool IsSynchronized
{
    get
    {
	return false;
    }
}

Implementation. You can see that the IsFixedSize, IsReadOnly, and IsSynchronized properties all return a constant boolean. These are defined in the Array type, which means all arrays will return these values. There is really no point of ever calling IsFixedSize, IsReadOnly, or IsSynchronized on an array type variable.

Question and answer

Why are they there? These properties are defined on the IList and ICollection interfaces. With IList, you are required to define IsFixedSize and IsReadOnly. With ICollection, you are required to have IsSynchronized. Therefore, if you have a variable reference of type IList or ICollection, these properties may actually be useful, because an Array is only one implementation of these interfaces.

Summary

The C# programming language

In this program, we looked at the IsFixedSize, IsReadOnly, and IsSynchronized properties on the Array type. If you know you are dealing with an array, these properties are never useful as they are constant. If you are dealing with one of the interfaces an array implements, they may be more useful.

Array Types
.NET