Why is .net said to be language independent




















The Common Language Specification requires all fields and methods to be accessed as members of a particular class. Therefore, global static fields and methods that is, static fields or methods that are defined apart from a type are not CLS-compliant. If you try to include a global field or method in your source code, both the C and Visual Basic compilers generate a compiler error.

The Common Language Specification supports only the standard managed calling convention. It doesn't support unmanaged calling conventions and methods with variable argument lists marked with the varargs keyword. For variable argument lists that are compatible with the standard managed calling convention, use the ParamArrayAttribute attribute or the individual language's implementation, such as the params keyword in C and the ParamArray keyword in Visual Basic.

Overriding an inherited member cannot change the accessibility of that member. For example, a public method in a base class cannot be overridden by a private method in a derived class. There is one exception: a protected internal in C or Protected Friend in Visual Basic member in one assembly that is overridden by a type in a different assembly.

In that case, the accessibility of the override is Protected. The following example illustrates the error that is generated when the CLSCompliantAttribute attribute is set to true , and Human , which is a class derived from Animal , tries to change the accessibility of the Species property from public to private.

The example compiles successfully if its accessibility is changed to public. Types in the signature of a member must be accessible whenever that member is accessible. For example, this means that a public member cannot include a parameter whose type is private, protected, or internal. The following example illustrates the compiler error that results when a StringWrapper class constructor exposes an internal StringOperationType enumeration value that determines how a string value should be wrapped.

Nested types always have at least as many generic parameters as their enclosing type. These correspond by position to the generic parameters in the enclosing type. The generic type can also include new generic parameters. The relationship between the generic type parameters of a containing type and its nested types may be hidden by the syntax of individual languages.

The calls to the ToString method, which each class inherits from Object. ToString , show that each nested class includes the type parameters of its containing class. This encoding of generic type names is primarily of interest to developers who use reflection to access CLS-complaint generic types in a library. If constraints are applied to a generic type, any types used as constraints must also be CLS-compliant.

The following example defines a class named BaseClass that is not CLS-compliant and a generic class named BaseCollection whose type parameter must derive from BaseClass. If a generic type is derived from a generic base type, it must redeclare any constraints so that it can guarantee that constraints on the base type are also satisfied. The Common Language Specification imposes a conservative per-instantiation model for nested types and protected members.

Open generic types cannot expose fields or members with signatures that contain a specific instantiation of a nested, protected generic type. Non-generic types that extend a specific instantiation of a generic base class or interface cannot expose fields or members with signatures that contain a different instantiation of a nested, protected generic type. N in Visual Basic. N or C1 Of Integer.

It has two methods, M3 and M4. Language compilers can be even more restrictive. In this example, Visual Basic displays an error when it tries to compile M4. A constructor of a derived class must call the instance constructor of its base class before it accesses inherited instance data. This requirement is because base class constructors are not inherited by their derived classes. This rule does not apply to structures, which do not support direct inheritance.

Typically, compilers enforce this rule independently of CLS compliance, as the following example shows. It creates a Doctor class that is derived from a Person class, but the Doctor class fails to call the Person class constructor to initialize inherited instance fields. An object constructor cannot be called except to create an object. In addition, an object cannot be initialized twice. For example, this means that Object. MemberwiseClone and deserialization methods such as BinaryFormatter.

Deserialize must not call constructors. A property must have a setter, a getter, or both. A property's type is the return type of the property getter and the last argument of the setter. These types must be CLS compliant, and arguments cannot be assigned to the property by reference that is, they cannot be managed pointers. If a property has both a getter and a setter, they must both be virtual, both static, or both instance.

The C and Visual Basic compilers automatically enforce this rule through their property definition syntax. An event is defined by its name and its type. The event type is a delegate that is used to indicate the event. For example, the AppDomain.

AssemblyResolve event is of type ResolveEventHandler. In addition to the event itself, three methods with names based on the event name provide the event's implementation and are marked as SpecialName in the assembly's metadata:.

For example, the event subscription method for the AppDomain. For example, the removal method for the AppDomain. Most of the Common Language Specification's rules regarding events are implemented by language compilers and are transparent to component developers. The methods for adding, removing, and raising the event must have the same accessibility. They must also all be static, instance, or virtual. The methods for adding and removing an event have one parameter whose type is the event delegate type.

The add and remove methods must both be present or both be absent. The following example defines a CLS-compliant class named Temperature that raises a TemperatureChanged event if the change in temperature between two readings equals or exceeds a threshold value. Members can be overloaded based on the number of parameters and the type of any parameter.

Calling convention, return type, custom modifiers applied to the method or its parameter, and whether parameters are passed by value or by reference are not considered when differentiating between overloads. For an example, see the code for the requirement that names must be unique within a scope in the Naming conventions section. These two operators can be overloaded based on both their parameters and their return value.

Exception objects must derive from System. Exception or from another type derived from System. The following example illustrates the compiler error that results when a custom class named ErrorClass is used for exception handling. To correct this error, the ErrorClass class must inherit from System. In addition, the Message property must be overridden. NET assemblies, custom attributes provide an extensible mechanism for storing custom attributes and retrieving metadata about programming objects, such as assemblies, types, members, and method parameters.

Custom attributes must derive from System. Attribute or from a type derived from System. The following example violates this rule. It defines a NumericAttribute class that does not derive from System. A compiler error results only when the non-CLS-compliant attribute is applied, not when the class is defined.

The constructor or the properties of a CLS-compliant attribute can expose only the following types:. The following example defines a DescriptionAttribute class that derives from Attribute. The class constructor has a parameter of type Descriptor , so the class is not CLS-compliant.

The C compiler emits a warning but compiles successfully, whereas the Visual Basic compiler doesn't emit a warning or an error. At compile time, the compiler detects non-compliant elements that are presumed to be CLS-compliant and emits a warning. The compiler does not emit warnings for types or members that are explicitly declared to be non-compliant.

To define the parts of the public interface exposed by a component that are CLS-compliant and the parts that are not CLS-compliant. When the attribute is used to mark particular program elements as CLS-compliant, its use guarantees that those elements are accessible from all languages and tools that target.

To ensure that the component library's public interface exposes only program elements that are CLS-compliant. If elements are not CLS-compliant, compilers will generally issue a warning. For example, defining a static member in an interface violates a CLS rule. In this regard, if you define a static in C or Shared in Visual Basic member in an interface, both the C and Visual Basic compilers display an error message and fail to compile the app.

This value allows you to apply the CLSCompliantAttribute attribute to any program element, including assemblies, modules, types classes, structures, enumerations, interfaces, and delegates , type members constructors, methods, properties, fields, and events , parameters, generic parameters, and return values.

However, in practice, you should apply the attribute only to assemblies, types, and type members. Otherwise, compilers ignore the attribute and continue to generate compiler warnings whenever they encounter a non-compliant parameter, generic parameter, or return value in your library's public interface.

You can explicitly override the inherited compliance by applying the CLSCompliantAttribute attribute to a contained program element. For example, you can use the CLSCompliantAttribute attribute with an isCompliant value of false to define a non-compliant type in a compliant assembly, and you can use the attribute with an isCompliant value of true to define a compliant type in a non-compliant assembly.

You can also define non-compliant members in a compliant type. However, a non-compliant type cannot have compliant members, so you cannot use the attribute with an isCompliant value of true to override inheritance from a non-compliant type. When you are developing components, you should always use the CLSCompliantAttribute attribute to indicate whether your assembly, its types, and its members are CLS-compliant.

If you've successfully marked all your non-compliant types and members, your compiler should not emit any non-compliance warnings. However, you should indicate which members are not CLS-compliant and list their CLS-compliant alternatives in your product documentation.

Because both members are tagged with the CLSCompliant false attribute, the compiler produces no warnings. The class also provides a CLS-compliant alternative for both methods. However, because methods cannot be overloaded based on return value, the names of the CLS-compliant methods are different from the names of the non-compliant methods. If you are developing an app rather than a library that is, if you aren't exposing types or members that can be consumed by other app developers , the CLS compliance of the program elements that your app consumes are of interest only if your language does not support them.

In that case, your language compiler will generate an error when you try to use a non-CLS-compliant element. Language independence has a few possible meanings.

One meaning involves seamlessly consuming types written in one language from an app written in another language. A second meaning, which is the focus of this article, involves combining code written in multiple languages into a single.

NET assembly. The following example illustrates cross-language interoperability by creating a class library named Utilities.

Here's the source code for StringUtil. NET framework facilitates many features, and developers can choose from a variety of programming languages available on the. Net platform. The main advantage with. So, we can say that. Net framework is the perfect solution for Business Application Development.

Want to know more? Home Services About Us Careers. What is. NET Framework? Common Language Runtime in. NET Framework Software programs written in. Value Type and Reference Type. NET languages work Visual Studio. Architecture of. Features of. Used for Service-Oriented Architecture.

Multi-tiered Software Architecture. Feature-rich There are a variety of features which will be explored by the developers to make powerful apps. Types of applications that can be created with. Notify of. Oldest Newest Most Voted. Inline Feedbacks. NET Lover. It makes most CLI languages very similar in their nature, but not quite: for example, F uses a very different paradigm of functional programming.

Posted Aug pm Sergey Alexandrovich Kryukov. Sergey Alexandrovich Kryukov Aug pm. Thank you, Manas. I wish I could do it better, it's just time and space is limited. The problem is not limited by one technology and not by technology along. Thank you, Prasad. In short, the answer is CLR. Posted Aug pm Manas Bhardwaj. Thx Prasad! Fair enough, my 5. Posted Aug pm Ganesh Nikam.

Ganesh Nikam Aug am. Copy Code. Posted Aug pm Aarti Meswania. Aarti Meswania Aug am. I can't get, what is reason for down-vote? Well, its common here. If they don't have reason for down-voting then they should avoid to vote answers. The world is full of cowards, and some of them only can do such things which you can't stop. You're welcome. Add your solution here. OK Paste as.

Treat my content as plain text, not as HTML. Existing Members Sign in to your account. This email is in use. Do you need your password? Submit your solution!



0コメント

  • 1000 / 1000