Interoperability Feature in C#
Interoperability is all about the ability of a system to communicate with components of another system. To understand it in detail, let’s consider the back end of an e-commerce system where there are two core functions -
- Maintaining the historical data of different orders
- Automatically packing and segregating the items based on the location of the package and where it has to be delivered.
Now let us assume that the maintenance of order info is being done using the C# language. On the other hand, the packing and segregation tool has been developed in C++.
Whenever the order is ready to be shipped, the item is placed in a conveyor belt which flows through the packaging system. It later gets segregated as required based on the location data. In order to track the package in the conveyor belt, a status has to be updated from the packaging system once the package crosses it. So, the packaging system which is a standalone system requires sending an update to the C#- based main codes that are running on the server.
Also, when the package reaches the segregation system, the segregation system needs the location to which the package needs to be moved to. So, now, the packaging system requires communication from the C++ system to the C# system. Similarly, the segregation system requires the data for the sent value from the C++ system to the C# system. This means we need to develop our application in a programming language that supports multiple systems and gets the job done for the company. Luckily, C# interoperability makes it easy to achieve the same. Let's see in detail what is the interoperability feature in C# and how it is useful.
Table of contents
- What is interoperability in C#?
- Interoperability possibilities in C#
What is interoperability in C#?
Interoperability is the property that enables different programming languages to interact within the same system. This helps to achieve a common objective and make use of the code which is being available already. In C#, when it comes to interoperability, the code that is being written using the C# language will be validated by the compiler that comes with the IDE, and the memory management for the code will also be taken care of.
Also, if a DLL is being referred to as an external reference and the external package is written in a programming language that can manage itself, then it is considered as the managed code. If that is not the case, then it is considered as an unmanaged code by the C# compiler. C# allows the inclusion of both managed and unmanaged codes as external references in the form of DLL.
Interoperability possibilities in C#
With Managed Code
Managed code refers to consuming the code written in VB.Net or C# with each other. For example, a feature is being developed in C#, and the same is required in a VB .Net project. Though they are from different programming languages, .Net provides the option to reference them by interchanging the places where they have been developed through the concept of DLL (Dynamic Link Library).
.Net provides options to create class libraries, by adding the class library project and writing the code logic inside it. Once built, the C# or VB.Net code generates the DLL of the functionality. This DLL can be easily added as a reference in both places, thereby serving the purpose of code reusability. It provides the added cost advantage of completing the functionality in the original language.
With UnManaged Code
To understand the interoperability options available in C# for unmanaged code, we need to know everything about COM. Component Object Model, commonly known as COM is one of the earliest invented technologies to allow binary reuse of components. It enables the possibility of providing the interfaces for the programs that are written in different programming languages. COM uses a GUID to identify the unique members that are bundled together. And it allows registering a single DLL for each GUID.
Consuming COM components from .Net projects: As we have seen above, a COM exposes interfaces to communicate. However, a .Net-based client cannot consume an interface exposed by COM directly as it cannot understand it. So, a wrapper has to be defined around the COM component in such a way that it makes the code understandable for the .Net client. Here, the wrapper is known as Runtime Callable Wrapper (RCW). The RCW can be generated through VS.NET and when the client calls the functionality in COM, the call goes through RCW and it translates to the native COM calling procedure. Also, when it comes to COM components, features such as inheritance, parameterized constructors, etc. cannot be utilized.
Consuming .Net components from COM clients: Let's consider the opposite scenario. Just like the Runtime Callable Wrapper which enables .Net clients to identify .Net components from COM objects, COM Callable Wrapper (CCW) has to be created in this case. It can be created using the RegAsm.exe which comes as a part of the .Net utility. When a call is being made from the COM components, the call gets routed through CCW which in turn converts the native COM calls to .Net understandable format and converts the response similarly.
.Net provides options to communicate between both Managed and UnManaged code. Communication in managed code is achieved through the direct reference of DLLs while in unmanaged code, it is achieved through the concept of RCW and CCW.