国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

C# FAQ for C++ programmers

2024-07-21 02:17:28
字體:
供稿:網(wǎng)友
  • 網(wǎng)站運(yùn)營seo文章大全
  • 提供全面的站長運(yùn)營經(jīng)驗(yàn)及seo技術(shù)!
  •  

    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 );

        }        

     
    發(fā)表評論 共有條評論
    用戶名: 密碼:
    驗(yàn)證碼: 匿名發(fā)表
    主站蜘蛛池模板: 新竹县| 新干县| 台中市| 江山市| 武城县| 东兴市| 富民县| 达日县| 灵宝市| 岐山县| 滁州市| 巴塘县| 垣曲县| 西藏| 怀宁县| 连平县| 庄浪县| 阿克| 广河县| 隆昌县| 贵州省| 万载县| 陵川县| 昌图县| 绥江县| 大庆市| 台南市| 博爱县| 乌兰浩特市| 新泰市| 裕民县| 湘阴县| 喜德县| 浑源县| 奈曼旗| 石景山区| 孟村| 大悟县| 秦皇岛市| 孟津县| 左权县|