|
The C# Column
Miscellaneous .NET features
In
this article we shall discuss a few .NET topics that are too small
to warrant a separate article devoted to each topic. However, these
topics provide certain useful programming features and could prove
to be of immense help in certain programming strategies.
Language interoperability
Language interoperability is a key feature of the .NET
environment. Language interoperability means that we can use an assembly written
in any .NET compliant language within a program written in another .NET compliant
language. This is all possible because all .NET compliant languages compile
into IL (Intermediate Language). Language interoperability supports code-reuse,
which is the most sought after feature in todays programming world.
To illustrate language interoperability we will create
an assembly in Managed C++ and then see how to use it through C#. Lets
first take a look at the assembly written in Managed C++. To create this, select
Visual C++ Projects and choose the Managed C++ Class Library
template. Name the class library as McppServer. The code to be written
in it is given below.
#include stdafx.h
namespace McppServer
{
public __gc class fact
{
public:
int factorial
( int n )
{
int j = n
;
for ( int
i = 1 ; i < n ; i++ )
j = j * i
;
return j ;
}
} ;
}
In the fact class we have written a method called factorial(
) that accepts a number and returns the factorial value of that number. The
__gc stands for garbage collected. It specifies that the underlying type would
be treated as a managed type. It is necessary to mention this because in Managed
C++ by default a type is unmanaged. On building this program an assembly called
McppServer.dll gets created.
Now lets look at the C# program. We have created a console
application called CSharpClient. To be able to use McppServer.dll
in this project, we have to add its reference through the Solution Explorer
window.
using System ;
using McppServer ;
namespace CSharpClient
{
class Class1
{
[
STAThread ]
static
void Main ( string [ ] args )
{
fact
f = new fact( ) ;
Console.WriteLine
( f.factorial ( 5 ) ) ;
}
}
}
We have added the statement using McppServer at the beginning
of the program. This gives us access to the classes defined in this namespace.
Here we have created an object of the fact class and used it to call the methods
of this class. On executing this program we get the output as 120.
Using pointers in .NET
Pointers can be used in C# only in the blocks of code that
we have specifically marked for pointer use. The keyword to do so is unsafe.
Unsafe because there are potential risks associated with pointers
such as memory leaks, overwriting of important information, stack overflow,
etc. Code that uses pointers is difficult to debug and will fail the memory
type safety checks imposed by the Common Language Runtime. We can mark individual
methods, classes, structures, blocks of code or even local variables as unsafe.
For example we can write:
unsafe float myfunction( )
{
// can use pointers
}
unsafe class myclass
{
unsafe int *x ;
}
unsafe
{
// unsafe block
}
Declaring pointers is different in C# than in C/C++. For
example, if we want to declare two pointers to ints, we write:
int *x, y ;
in C# as, against
int *x, *y ;
in C/C++.
Once weve declared the pointers, we can use them
as normal pointer variables using the address of (&) and the
value at address (*) operators.
For the most part, C# relies on references of instances
of classes, and the language has been designed in such a way that pointers are
not required as often as they are in C/C++.
The checked and unchecked operators
The checked and unchecked operators can be used to control
overflow checking for arithmetic operations and conversions of integer types.
If a statement is marked as checked, the CLR enforces overflow checking and
throws an exception if an overflow occurs. On the other hand, if it is marked
unchecked, and overflow occurs, the overflowing bits (higher) are discarded
and exception is not thrown. For example, consider the following snippet:
byte b = 255 ;
checked
{
b++ ;
}
Here an exception will be thrown. But the following would
not throw an exception:
byte b = 255 ;
unchecked
{
b++ ;
}
Here the overflowing bits would be lost and b would
contain 0. By default, all the operations are unchecked.
 |
Yashavant Kanetkar, one of the first
Express Computer columnists, is an established software expert,
speaker and author with several best-sellers to his credit,
including titles like “Let Us C” and the “Fundas” series. Contact
him at kanetkar@dcubesoft.com |
|