Advantages of Using C#
Advantages of Using C#
Even expert programmers can make the simplest of mistakes-forgetting to initialize a variable, for instance and often those simple mistakes result in unpredictable problems that can remain undiscovered for long periods of time. Once a program is in production use, it can be very costly to fix even the simplest programming errors.
The modern design of C# eliminates the most common C++ programming errors . For example:
- Garbage collection relieves the programmer of the burden of manual memory management.
- Variables in C# must be initialized before using them.
- Variables are type-safe. You can't make unsafe casts.
- Array bounds checking is built-in.
- No pointers.
- Consistent error handling through exceptions.
- Unified type system. All variables are objects.
The end result is a language that makes it far easier for developers to write and maintain programs that solve complex business problems.
Unified Type System
Types in .NET are consistent regardless of the language. In the past, a VB floating point variable couldn't be handled by a C/C++ program. C/C++ just didn't know how to handle it. In fact, the size of an integer was different in C/C++ depending on how it was compiled. This makes it hard for developers because if they don't know what size and integer is then how can they process it?
In .NET all basic data types are a consistent size regardless of the language or the machine.
Consistent Error Handling
In any large application, you will have pieces of your software written in different languages. You might choose to useVB to do your user interface because it is quick and easy to build and you might choose C++ to do your computations because it can easily handle complex data structures.
The problem lies in error handling. VB uses one form of error handling and C++ uses a different form of error handling. Why is this a problem? Well what happens if the VB user interface asks the C++ computation engine to calculate a value and in the process of calculating the value, C++ finds an error. How does the C++ portion send an error back to the VB user interface? And vice-versa, what if C++ asks VB to display a window and VB finds an error, how does VB report back to the C++ code that an error happened? Both VB and C++ have very good error handling abilities but they only work within the language, they don't work across different languages.
In .NET we have an Exception object which is the foundation for error handling. Again, regardless of what language you use, the Exception object will work to communicate errors.
Real-world experience shows that some applications continue to require "native" code, either for performance reasons or to interoperate with existing application programming interfaces (APIs). Such scenarios may force developers to use C++ even when they would prefer to use a more productive development environment.
C# addresses these problems by:
- Including native support for the Component Object Model (COM) and Windows-based APIs.
- Allowing restricted use of native pointers.
In C#, every object is automatically a COM object. Developers no longer have to explicitly implement IUnknown and other COM interfaces. Instead, those features are built in. Similarly, C# programs can natively use existing COM objects, no matter what language was used to author them.
For those developers who require it, C# includes a special feature that enables a program to call out to any native API. Inside a specially marked code block, developers are allowed to use pointers and traditional C/C++ features such as manually managed memory and pointer arithmetic. This is a huge advantage over other environments. It means that C# programmers can build on their existing C and C++ code base, rather than discard it.
In both cases-COM support and native API access-the goal is to provide the developer with essential power and control without having to leave the C# environment.