Destination .NET! Platform Tools, Technologies & Resources
1 3 5 7 9
2 4 6 8 10
From VB4, C++ and Java to working on .Net Since Beta, 1.0.
Improving partner satisfaction and accelerating Microsoft platform adoption for managed ISVs.
Is your application compatible with Windows Vista? Make sure today by taking our self-test. Just follow the five steps of the Works with Windows Vista program so that you and your customers can be confident in your solution’s compatibility. Read More >>
What product/topic are you most interested in?
(Choose your top answer.)
Windows Vista
Windows Server 2008
2007 Microsoft Office system
SQL Server 2008
ASP.NET
Visual Studio 2008
Windows Mobile
Software as a Service
A little bit of everything
Just browsing, thanks

View Results
Whether you love the site or hate it, we want to know. Tell us what topics to cover, help us improve things, or just sound off on something we could've done better. Send your feedback directly to the editor by email.
 Print Print
Average Rating: 4.3/5 | Rate this item | 4 users have rated this item.
Moving from C/C++ to C#: What to Expect
They look alike—do they play alike? Well, somewhat—but there are many subtle differences that will trap you until you get the hang of it. Eric Gunnerson gives you a rundown that will likely save you a lot of headscratching—and maybe a landmine or two. 

Developers who are thinking of migrating C/C++ code to C# will be pleasantly surprised to see how much the languages look alike. This similarity might suggest that the only necessary changes are in the APIs and library calls. However, there are subtle differences in the languages and in their implementation. This article presents a brief overview of the major syntactical and semantic differences between the languages, so that you don't spend your time puzzling over code that won't compile or, worse yet, so you won't accidentally step on a landmine.

A Managed Environment

  • C# runs in the .NET Runtime environment. This not only means that there are many things that aren't under the programmer's control, it also provides a brand-new set of frameworks.

  • Object deletion is performed by the garbage collector sometime after the object is no longer used. Destructors (a.k.a. finalizers) can be used for some cleanup, but not in the way that C++ destructors are used.

  • There are no pointers in the C# language. (Actually, there are in unsafe mode, but they are rarely used.) References are used instead, and they are similar to C++ references without some of the C++ limitations.

  • Source is compiled to assemblies, which contain both the compiled code (expressed in the .NET intermediate language, IL) and metadata to describe that compiled code. All .NET languages query the metadata to determine the same information that is contained in C++ header files, and so #include files are therefore not used.

  • Calling native code requires coding overhead and is not recommended.

  • There is no C/C++ runtime library nor any direct equivalent. The same operations-such as string manipulation, file I/O, and other routines-can be done with the .NET runtime and are found in the namespaces that start with System.

  • Exception handling is used instead of error returns.

    .NET Objects

  • C# objects all have the ultimate base class object; there is only single inheritance of classes, although there is multiple implementation of interfaces.

  • Lightweight objects, such as data types, can be declared as structs (also known as value types), which means they are allocated on the stack instead of the heap.

  • C# structs and other value types (including the built-in data types) can be used in situations where objects are required by boxing them, which automatically copies the value into a heap-allocated wrapper that is compliant with heap-allocated objects (also known as reference objects). This unifies the type system, allowing any variable to be treated as an object, but without overhead when unification isn't needed.

  • C# supports properties and indexers to separate the user model of an object from the implementation of the object, and it supports delegates and events to encapsulate function pointers and callbacks.

  • C# provides the params keyword to provide support similar to varags.

    C# Statements
    C# statements correspond closely to their C++ counterparts. There are a few notable differences:

  • The new keyword means "obtain a new instance of." The object is heap- allocated if it is a reference type, and stack or inline allocated if it is a value type.

  • All statements that test a boolean condition now require a variable of type bool. There is no automatic conversion from int to bool, so if (i) is not valid.

  • To reduce errors, switch statements disallow fall-through. switch statements can be used on string values.

  • foreach can be used to iterate over objects and collections.

  • Checked and unchecked specify whether arithmetic operations and conversions are checked for overflow.

  • Definite assignment requires that objects have a definite value before being used.

    Attributes
    Attributes are annotations written to convey declarative data from the programmer to other code. That other code might be the runtime environment, a designer, a code analysis tool, or some other custom tool. Attribute information is retrieved through a process known as reflection.

    Attributes are written inside of square brackets, and can be placed on classes, members, parameters, and other code elements. Here's an example:

    	[CodeReview("1/1/199", Comment="Rockin'")]
    	class Test
    	{
    	}
    

    Code Organization
    C# has no header files. All code is written inline, and while there is preprocessor support for conditional code, there is no support for macros. These restrictions make it both easier and faster for the compiler to parse C# code, and also make it easier for development environments to understand C# code.

    In addition, there is no order dependence in C# code and no forward declarations exist. The order of classes in source files is unimportant; classes can be rearranged at will.

    Missing C# Features
    The following C++ features don't exist in C#:

    • Multiple inheritance
    • const member functions or parameters. However, const fields are supported.
    • Global variables
    • typedef
    • Conversion by construction
    • Default arguments on function parameter
    References
  • Making the move from C++ to C#
  • How to Wrap C++ Classes to Make them Accessible Under C#
  • Page 1 of 1
    This article is adapted with permission from Eric Gunnersons book A Programmers Introduction to C#, 2nd Edition from A!Press. A sample chapter and table of contents are available at http://apress.com/book/supplementDownload.html?bID=83&sID=776.
    Submit article to:
    Extending your solution to run on Microsoft technology is easier than ever. Through NXT, you can reach more customers, increase revenues and slash development time and costs, accelerating both your time to market and profitability. Get the details on NTX. >>
    Sign up for your free e-mail newsletters today!
    DevX Windows Developer Update

    More Newsletters
    Taming Trees: Building Branching Structures
    Implement Drag and Drop in Your Windows Forms Applications
    Performing CRUD and Grouping Operations with the LinqDataSource Control
    Override a Child Class' Parent Method
    Keeping Your Directory Structure Intact After Deleting Folder Contents



    JupiterOnlineMedia

    internet.com earthweb.com Devx.com mediabistro.com Graphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info

    Copyright 2008 Jupitermedia Corporation All Rights Reserved.
    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers