C#: Nonzero-based arrays are not CLS-compliant
Asked Answered
F

4

11

I am currently reading Albahari's C# 3.0 in a Nutshell and on pg. 241, whilst talking about Array indexing, he says this:

Nonzero-based arrays are not CLS (Common Language Specification)-compliant

What does it mean exactly, for nonzero arrays to not be CLS compliant ? And what implications does it have on your code?

[Update]

Here is a link to the page of the book.

Fortieth answered 5/6, 2009 at 13:5 Comment(3)
Others I feel have answered the question adequately. If you wish to play around with CLSCompliance in your code- try experimenting with the CLSCompliant attribute- msdn.microsoft.com/en-us/library/…Benedicite
That's an bit of an odd thing to say, though perhaps it makes more sense in the context of the rest of the page. There's no obvious way to make a non-zero-based array in C#, so why mention that it's not CLS compliant?Breton
Eric, I posted a link to the page in the book in the post nowFortieth
J
27

The CLS (Common Language Specification) lays the groundwork for a common set of rules for compliance that guarantees that other languages (VB.NET, F#, etc.) can use assemblies that you have built with C#. A nonzero-based array would not be compliant as other languages expect arrays to be zero-based.

Here is an example that is easier to understand:

class Foo
{
    public void Bar() { }
    public void bar() { } 
}

This type would not be CLS compliant since it contains two members that differ in name only by type. How would someone using VB.NET disambiguate between Bar and bar since the VB.NET compiler is not case-sensitive?

So basically the CLS is a bunch of rules like this to guarantee interoperability between languages.

Jobi answered 5/6, 2009 at 13:7 Comment(0)
A
11

CLS compliance is mostly about making sure that your code is as broadly compatible with other languages as possible. It includes things like not exposing public members which differ only by case (which would confuse VB, which is case-insensitive). See this MSDN article for more information, along with the common language specification itself.

Approximal answered 5/6, 2009 at 13:7 Comment(0)
I
1

I addition to what's been said, nonzero-based arrays exist solely to ease transition for existing VB6 code (mainly by the automatic migration tool) since in VB6, array indexing could start from an arbitrary number, not necessarily zero.

Due to the CLS compliance issue (and other considerations), it's not recommended to ever use them in .NET (even when programming VB.NET). Furthermore, their use is rather restricted. It's easier just to do an offset translation by encapsulating the array inside a class and writing an appropriate index access operator.

Indebtedness answered 5/6, 2009 at 15:12 Comment(0)
F
0

Also,

If your application is not intended to work with other programs - as in it is a self-contained unit that you won't sell as a public class library to other people, do not worry about it too much.

But the other comments here are correct when developing a generic class library.

It is always good practice to use [assembly:CLSCompliant(true)], but it isn't critical to getting your application running.

Fortepiano answered 5/8, 2009 at 17:14 Comment(1)
John said it is about complying with other languages. I stated that it isn't necessary to fix this warning if your application is an isolated application. I didn't think John stated that, hence leaving the comment. Otherwise the author could have started going through the application unnecessarily making it compliant.Fortepiano

© 2022 - 2024 — McMap. All rights reserved.