首页 > 开发 > 综合 > 正文

C# FAQ for C++ programmers

2024-07-21 02:17:28
字体:
来源:转载
供稿:网友
  • 网站运营seo文章大全
  • 提供全面的站长运营经验及seo技术!
  •  

    c# faq for c++ programmers

    1. what is c#?[introduction]

    c# is a programming language designed by microsoft. it is loosely based on c/c++, and bears a striking similarity to java. microsoft describe c# as follows:
    "c# is a simple, modern, object oriented, and type-safe programming language derived from c and c++. c# (pronounced 'c sharp') is firmly planted in the c and c++ family tree of languages, and will immediately be familiar to c and c++ programmers. c# aims to combine the high productivity of visual basic and the raw power of c++."

    2. how do i develop c# apps? [introduction]

    the (free) .net sdk contains the c# command-line compiler (csc.exe). visual studio has fully integrated support for c# development. on linux you can use mono.

    3. does c# replace c++?[introduction]

    there are three options open to the windows developer from a c++ background:
    ----stick with standard c++. don't use .net at all.
    ----use c++ with .net. microsoft supply a .net c++ compiler that produces il rather than machine code. however to make full use of the .net environment (e.g. garbage collection), a set of extensions are required to standard c++. in .net 1.x this extended language is called managed extensions for c++. in .net 2.0 me c++ has been completely redesigned under the stewardship of stan lippman, and renamed c++/cli.
    ----forget c++ and use c#.
    each of these options has merits, depending on the developer and the application. for my own part, i intend to use c# where possible, falling back to c++ only where necessary. me c++ (soon to be c++/cli) is very useful for interop between new .net code and old c++ code - simply write a managed wrapper class using me c++, then use the managed class from c#. from experience, this works well.

    4. does c# have its own class library? [introduction]

    not exactly. the .net framework has a comprehensive class library, which c# can make use of. c# does not have its own class library.

    5. what standard types does c# use?[types]

    c# supports a very similar range of basic types to c++, including int, long, float, double, char, string, arrays, structs and classes. however, don't assume too much. the names may be familiar, but many of the details are different. for example, a long is 64 bits in c#, whereas in c++ the size of a long depends on the platform (typically 32 bits on a 32-bit platform, 64 bits on a 64-bit platform). also classes and structs are almost the same in c++ - this is not true for c#. finally, chars and strings in .net are 16-bit (unicode/utf-16), not 8-bit like c++.
    furthermore,these simple built-in types are just aliases to the according .net framework classes.for instance,bool for system.boolean ,char for system.char,int for system.int32,float for system.single,double for system.double and long for system.int64

    6. is it true that all c# types derive from a common base class? [types]

    yes and no. all types can be treated as if they derive from object (system.object), but in order to treat an instance of a value type (e.g. int, float) as object-derived, the instance must be converted to a reference type using a process called 'boxing'. in theory a developer can forget about this and let the run-time worry about when the conversion is necessary, but in reality this implicit conversion can have side-effects that may trip up the unwary.

    7. so i can pass an instance of a value type to a method that takes an object as a parameter? [types]

    yes. for example:

        class capplication

        {

            public static void main()

            { 

                int x = 25; 

                string s = "fred"; 

            

                displayme( x ); 

                displayme( s ); 

            } 

        

            static void displayme( object o ) 

            { 

                system.console.writeline( "you are {0}", o ); 

            }

        } 

    this would display:

        you are 25

        you are fred

    8. what are the fundamental differences between value types and reference types?[types]

    c# divides types into two categories - value types and reference types. most of the intrinsic types (e.g. int, char) are value types. structs are also value types. reference types include classes, arrays and strings. the basic idea is straightforward - an instance of a value type represents the actual data, whereas an instance of a reference type represents a pointer or reference to the data.
    the most confusing aspect of this for c++ developers is that c# has predetermined which types are represented as values, and which are represented as references. a c++ developer expects to take responsibility for this decision.

    for example, in c++ we can do this:

        int x1 = 3;        // x1 is a value on the stack

        int *x2 = new int(3)    // x2 is a pointer to a value on the heap

    but in c# there is no control:

        int x1 = 3;        // x1 is a value on the stack

        int x2 = new int(); 

    x2 = 3;        // x2 is also a value on the stack!

     

    9. okay, so an int is a value type, and a class is a reference type. how can int be derived from object? [types]

    it isn't, really. when an int is being used as an int, it is a value. however, when it is being used as an object, it is a reference to an integer value (on the managed heap). in other words, when you treat an int as an object, the runtime automatically converts the int value to an object reference. this process is called boxing. the conversion involves copying the int to the heap, and creating an object instance which refers to it. unboxing is the reverse process - the object is converted back to a value.

        int x = 3;        // new int value 3 on the stack

        object objx = x;    // new int on heap, set to value 3 - still have x=3 on stack

        int y = (int)objx;    // new value 3 on stack, still got x=3 on stack and objx=3 on heap

    10.             are c# references the same as c++ references? [types]

    not quite. the basic idea is the same, but one significant difference is that c# references can be null . so you cannot rely on a c# reference pointing to a valid object. in that respect a c# reference is more like a c++ pointer than a c++ reference. if you try to use a null reference, a nullreferenceexception is thrown.

    for example, look at the following method:

        void displaystringlength( string s )

        {

            console.writeline( "string is length {0}", s.length );

        }        

    the problem with this method is that it will throw a nullreferenceexception if called like this:

        string s = null;

        displaystringlength( s );

    of course for some situations you may deem a nullreferenceexception to be a perfectly acceptable outcome, but in this case it might be better to re-write the method like this:

        void displaystringlength( string s )

        {

            if( s == null )

                console.writeline( "string is null" );

            else

                console.writeline( "string is length {0}", s.length );

        }        

     
    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表