Chapter146:CLSCompliantAttribute

No Comments

Section146.1:AccessModifiertowhichCLSrulesapply

usingSystem;

[assembly:CLSCompliant(true)]namespa

ceCLSDoc

{

publicclassCat

{

internalUInt16_age=0;

privateUInt16_daysTillVacination=0;

//WarningCS3003    Typeof’Cat.DaysTillVacination’isnotCLS-compliant

protectedUInt16DaysTillVacination

{

get{return_daysTillVacination;}

}

//Warning         CS3003       Typeof’Cat.Age’isnotCLS-compliant

publicUInt16Age

{get{return_age;}}

//validbehaviourbyCLS-compliantrules

publicintIncreaseAge()

{

intincreasedAge=(int)_age+1;

returnincreasedAge;

}

}

}

TherulesforCLScomplianceapplyonlytoapublic/protectedcomponents.

Section146.2:ViolationofCLSrule:Unsignedtypes/sbyte

usingSystem;

[assembly:CLSCompliant(true)]namespa

ceCLSDoc

{

publicclassCar

{

internalUInt16_yearOfCreation=0;

//WarningCS3008    Identifier’_numberOfDoors’isnotCLS-compliant

//WarningCS3003    Typeof’Car._numberOfDoors’isnotCLS-compliant

publicUInt32_numberOfDoors=0;

//Warning          CS3003       Typeof’Car.YearOfCreation’isnotCLS-compliant

publicUInt16YearOfCreation

{

get{return_yearOfCreation;}

}

//WarningCS3002     Returntypeof’Car.CalculateDistance()’isnotCLS-compliant

publicUInt64CalculateDistance()

{

return0;

}

//WarningCS3002     Returntypeof’Car.TestDummyUnsignedPointerMethod()’isnotCLS-compliant

publicUIntPtrTestDummyUnsignedPointerMethod()

{

int[]arr={1,2,3,4,5,6,7,8,9,10};

UIntPtrptr=(UIntPtr)arr[0];

returnptr;

}

//WarningCS3003     Typeof’Car.age’isnotCLS-compliant

publicsbyteage=120;

}

}

Section146.3:Violation ofCLS rule:Same naming

usingSystem;

[assembly:CLSCompliant(true)]names

paceCLSDoc

{

publicclassCar

{

//Warning CS3005 Identifier’Car.CALCULATEAge()’differingonlyincaseisnotCLS-

compliant

publicintCalculateAge()

{

return0;

}

publicintCALCULATEAge()

{

return0;

}

}

}

Visual Basic is not case sensitive

Section146.4:ViolationofCLSrule:Identifier_

usingSystem;

[assembly:CLSCompliant(true)]namespa

ceCLSDoc

{

publicclassCar

{

//WarningCS3008    Identifier’_age’isnotCLS-complian

publicint_age=0;

}

}

You can not start variablewith _

Section 146.5: Violation of CLS rule: Inherit from non CLSComplaint class

usingSystem;

[assembly:CLSCompliant(true)]namespa

ceCLSDoc

{

[CLSCompliant(false)]

publicclassAnimal

{

publicintage=0;

}

//Warning         CS3009       ‘Dog’:basetype’Animal’isnotCLS-compliant

publicclassDog:Animal

{

}

}

About us and this blog

We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

Request a free quote

We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

Subscribe to our newsletter!

More from our blog

See all posts
No Comments

Leave a Comment