With so many new terms flying about, it is appropriate to clear the air before moving on. Table 29.1 defines some terms which will be needed in the discussion that follows.
Term | Meaning |
---|---|
Managed codes | Codes which run within the confines of the .NET CLR. Managed codes are first compiled into IL codes, then executed on-the-fly by the CLR. All your C# codes are managed – you cannot write unmanaged codes using C#. |
Unmanaged codes | Codes which do not run in the CLR, and are totally independent of it. Unmanaged codes will be native codes written for a specific operating system. Examples include legacy DLL files which run on top of the Win32 API directly. |
Safe codes | Managed codes which have type safety and security embedded within. All the C# codes you have seen up to now are considered safe codes. |
Unsafe codes | Managed codes which involve 'unsafe' operations, such as pointer operations which access memory directly. Unsafe codes must be marked by the unsafe modifier. Unsafe codes are seldom written by most application developers, unless they need the power provided by pointer operations. It is not possible to write unsafe codes in most .NET languages. C# and C++ allow you to write unsafe codes, while VB .NET and J# do not have such a feature. |
One important thing to understand is that both safe and unsafe codes are considered to be managed codes. Despite the use of pointer variables and other 'unsafe' stuff, unsafe codes still run within the confines of the .NET CLR (see Figure 29.1).
C# has three special keywords for writing unsafe codes only: sizeof, stackalloc, and fixed. Both the fixed and sizeof keywords will be covered in this chapter, but stackalloc is outside the scope of this book.
3.138.143.63