TheDeveloperBlog.com


C# Array IsFixedSize, IsReadOnly and IsSynchronized

IsFixedSize, IsReadOnly. The Array type provides several properties. Some of these help us detect features. These include IsFixedSize, IsReadOnly and IsSynchronized. What do these properties do and why are they there?


Example. In the following block, we see three parts. First we see the C# program that uses the three properties. Next we see the output of the program. And finally we see the implementation of the properties in the .NET Framework.

C# program that these properties

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;
    }
}

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.

So: There is really no point of ever calling IsFixedSize, IsReadOnly, or IsSynchronized on an array type variable.

Bool Type: If True, FalseFalse

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 be useful.


Summary. 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.

Tip: If you are dealing with one of the interfaces an array implements, they may be more useful.

Interfaces Types, IEnumerable