Author | Message | Time |
---|---|---|
Mephisto | What are my advantages of learning C# if I already know C++ to those experienced in it? What would I use C# for that I wouldn't use C++ for and vice versa? Are there key things in C# which make it superior to C++ or vice versa? Does C# have access to the Win32 API as C++ does? Personal view points? | December 8, 2004, 2:16 AM |
Myndfyr | [quote author=Mephisto link=topic=9821.msg91453#msg91453 date=1102472174] What are my advantages of learning C# if I already know C++ to those experienced in it? What would I use C# for that I wouldn't use C++ for and vice versa? Are there key things in C# which make it superior to C++ or vice versa? Does C# have access to the Win32 API as C++ does? Personal view points? [/quote] C# = C++ + Java. C# has access to the Win32 API but you have to code in your constants, which can, if you have a lot of them, be a big hassle. C# is very nice for rapid development in that you do not need to worry about memory management (in terms of dynamic allocation), and you don't need to deal with pointers; all class-based objects are passed by reference automatically. However, it's also nice that you still have the opportunity to use pointers, as I made available as an option for using unsafe code in my CRC32 library and used for a little while in a byte buffer for fast copying. Speed is also not a major issue, as each method is only JITted to native code once during the lifetime of the assembly (that is, until the assembly is actually changed), but if you're making lots of unmanaged calls (to COM or to C DLLs), you'll slow down. One of the major learning issues that you'd have transitioning over from C++ to C# is trying to use too much WinAPI in your code where managed methods exist to do the same thing. I've also seen silly C# new people who try to double-reference class instances and stuff as parameters, which is just redundant. Overall, I think Java programmers would have an easier time with C# than C++ programmers would; in fact, I have stated many times that C# is really just a superset of Java. It's Java plus the ability to call methods on value-types and native types, the use of pointers, the use of events, and function pointers that are type-safe. Attributed programming, too, which is a nice touch. (C# does not provide an inherent way to make unions like in C/++, but you can do that with the following construction that uses attributes: [code] [StructLayout(LayoutKind.Explicit)] private struct LGINT_HELPER { [FieldOffset(0)] uint low_dword; [FieldOffset(4)] int high_dword; [FieldOffset(0)] long qword; } [/code] which lets you lay out these things in memory). | December 8, 2004, 2:45 AM |
Mephisto | Hmm, okay. So for a lot of Win32 methods, there's built in methods avaliable to C# to accomplish the same thing? Also, one dying question I've always had when I looked at pieces of C# code (kind of silly), what's with the use of the keyword 'public' in classes and such? | December 8, 2004, 3:22 AM |
Myndfyr | [quote author=Mephisto link=topic=9821.msg91466#msg91466 date=1102476121] Hmm, okay. So for a lot of Win32 methods, there's built in methods avaliable to C# to accomplish the same thing? Also, one dying question I've always had when I looked at pieces of C# code (kind of silly), what's with the use of the keyword 'public' in classes and such? [/quote] I can't recall what the default behavior of C# classes is for member fields, but it's good for them to be private, protected, or internal, shielded from the properties or methods exposed as an interface. | December 8, 2004, 6:11 AM |
Kp | [quote author=Mephisto link=topic=9821.msg91466#msg91466 date=1102476121]Also, one dying question I've always had when I looked at pieces of C# code (kind of silly), what's with the use of the keyword 'public' in classes and such?[/quote] public => anyone can use/call it protected => this class and derived classes private => this class only Placing the modifier on every keyword is a Java idea that apparently C# inherited. Quite a pity. It's much nicer to specify access once and then have it persist for a while. Myndfyre, is such a feature available or is it required that every single declaration remind the compiler what protection it has? | December 8, 2004, 8:42 PM |
Myndfyr | [quote author=Kp link=topic=9821.msg91530#msg91530 date=1102538562] Myndfyre, is such a feature available or is it required that every single declaration remind the compiler what protection it has? [/quote] You can declare multiple fields of the same type: [code] private int n1, n2, n3; [/code] But you don't need to mark Class members as private; they are by default (like in C++). [code] public class Class1 { int Number; } public class Class2 { public Class2() { Class1 obj = new Class1(); obj.Number = 4; } } c:\playground\securedlibrary\class2.cs(16,4): error CS0122: 'SecuredLibrary.Class1.Number' is inaccessible due to its protection level [/code] Change it to a struct though: [code] public struct Class1 { int Number; } public class Class2 { public Class2() { Class1 obj = new Class1(); obj.Number = 4; } } c:\playground\securedlibrary\class2.cs(16,4): error CS0122: 'SecuredLibrary.Class1.Number' is inaccessible due to its protection level [/code] Unlike C++, Struct fields are ALSO private by default. This behavior is to enforce effective encapsulation practices. | December 8, 2004, 11:12 PM |
Eibro | So what is the difference between a struct and a class in C#? | December 8, 2004, 11:29 PM |
Adron | [quote author=Mephisto link=topic=9821.msg91466#msg91466 date=1102476121] Hmm, okay. So for a lot of Win32 methods, there's built in methods avaliable to C# to accomplish the same thing? Also, one dying question I've always had when I looked at pieces of C# code (kind of silly), what's with the use of the keyword 'public' in classes and such? [/quote] Umm... Didn't you say you knew C++ ? | December 8, 2004, 11:34 PM |
Mephisto | Yes, but I've never seen public class Something in C++. If you can do that I sure didn't know about it. | December 8, 2004, 11:59 PM |
Myndfyr | [quote author=Eibro[yL] link=topic=9821.msg91561#msg91561 date=1102548546] So what is the difference between a struct and a class in C#? [/quote] A structure is passed by value on the stack -- all of its members are deep-copied. A class is passed by reference -- only a reference is passed on the stack. [quote author=Mephisto link=topic=9821.msg91568#msg91568 date=1102550342] Yes, but I've never seen public class Something in C++. If you can do that I sure didn't know about it. [/quote] Oh, I didn't know you meant that. In C# and Java, classes can have public scope, protected scope, and private scope. Top-level classes can only be public (that is, classes that are the main class in Java, or any class directly within a namespace in .NET). Protected classes are actually classes within classes and can be accessed by classes that derive from the class that it exists within; private classes also can only exist within another class, but are only accessible from that container. .NET adds another protection level, internal, which allows types within that assembly to access it. Note that internal classes that implement public interfaces can still be used outside of said assembly though an interface reference. | December 9, 2004, 1:01 AM |
R.a.B.B.i.T | [quote author=Mephisto link=topic=9821.msg91568#msg91568 date=1102550342] Yes, but I've never seen public class Something in C++. If you can do that I sure didn't know about it. [/quote][code]class ClassName { public: Bob(); // constructor ~Bob(); // destructor DoStuff(char * things[]); // just some function private: DoSecretStuff(char * things[]); // secretive stuff! };[/code]That's off the top of my head, but I think it's right. Also, I've never used protected in C++, I'll have to toy with that :) | December 9, 2004, 1:54 AM |
Newby | [quote author=R.a.B.B.i.T link=topic=9821.msg91589#msg91589 date=1102557266] [quote author=Mephisto link=topic=9821.msg91568#msg91568 date=1102550342] Yes, but I've never seen public class Something in C++. If you can do that I sure didn't know about it. [/quote][code]class ClassName { public: Bob(); // constructor ~Bob(); // destructor DoStuff(char * things[]); // just some function private: DoSecretStuff(char * things[]); // secretive stuff! };[/code]That's off the top of my head, but I think it's right. Also, I've never used protected in C++, I'll have to toy with that :) [/quote] He means something like [code]public class ClassName { ... };[/code] | December 9, 2004, 2:13 AM |
R.a.B.B.i.T | Ahhh, I misinterpreted 90% of the discussion then.. | December 9, 2004, 2:53 AM |
Adron | [quote author=MyndFyre link=topic=9821.msg91582#msg91582 date=1102554069] In C# and Java, classes can have public scope, protected scope, and private scope. Top-level classes can only be public (that is, classes that are the main class in Java, or any class directly within a namespace in .NET). Protected classes are actually classes within classes and can be accessed by classes that derive from the class that it exists within; private classes also can only exist within another class, but are only accessible from that container. [/quote] What about C++: class sometoplevelclass { public: class publicclass { int bla; } instance1; private: class privateclass { int ble; } instance2; protected: class protectedclass { int blu; } instance3; }; | December 9, 2004, 8:10 PM |
Kp | [quote author=MyndFyre link=topic=9821.msg91558#msg91558 date=1102547542] [quote author=Kp link=topic=9821.msg91530#msg91530 date=1102538562] Myndfyre, is such a feature available or is it required that every single declaration remind the compiler what protection it has? [/quote] You can declare multiple fields of the same type: [code] private int n1, n2, n3; [/code][/quote] That's not quite what I meant. I was wanting something that is the C# equivalent of: [code] class A { public: int X; float y; char z; }; // all members of this class are globally accessible[/code]but without needing to repeat public on every single variable. | December 9, 2004, 9:25 PM |
Myndfyr | Then nope. There isn't. | December 9, 2004, 9:58 PM |
Mephisto | ew @ redundancy! | December 9, 2004, 11:40 PM |
kamakazie | [quote author=MyndFyre link=topic=9821.msg91657#msg91657 date=1102629482] Then nope. There isn't. [/quote] It's all just syntactic sugar anyways. | December 10, 2004, 5:20 AM |
KrAzY_NuK | [quote author=MyndFyre link=topic=9821.msg91582#msg91582 date=1102554069] Protected classes are actually classes within classes and can be accessed by classes that derive from the class that it exists within;[/quote] In Java, aren't protected classes accessible by all the other classes in the *package*? | December 20, 2004, 7:16 PM |
Myndfyr | [quote author=KrAzY_NuK link=topic=9821.msg93045#msg93045 date=1103570160] [quote author=MyndFyre link=topic=9821.msg91582#msg91582 date=1102554069] Protected classes are actually classes within classes and can be accessed by classes that derive from the class that it exists within;[/quote] In Java, aren't protected classes accessible by all the other classes in the *package*? [/quote] Say you have: A.java: [code] public class A { protected class B { } } [/code] C.java: [code] public class C extends A { // I can access B here. } [/code] That is Java protected-class accessibility. | December 20, 2004, 7:34 PM |
KrAzY_NuK | [quote author=MyndFyre link=topic=9821.msg93047#msg93047 date=1103571242] [quote author=KrAzY_NuK link=topic=9821.msg93045#msg93045 date=1103570160] [quote author=MyndFyre link=topic=9821.msg91582#msg91582 date=1102554069] Protected classes are actually classes within classes and can be accessed by classes that derive from the class that it exists within;[/quote] In Java, aren't protected classes accessible by all the other classes in the *package*? [/quote] Say you have: A.java: [code] public class A { protected class B { } } [/code] C.java: [code] public class C extends A { // I can access B here. } [/code] That is Java protected-class accessibility. [/quote] oops..i was thinking of data members A.java: [code] public class A { protected int B; } [/code] C.java: [code] public class C { // create an instance of A, and access B } [/code] Because you haven't explicitly named what package their in, their a part of the default package. From what i've learnt you should be able to access B from C. That's what I was asking. Now, given that, do the same rules apply to the classes? ergh, i'll have to check that out at home tonight. | December 20, 2004, 9:21 PM |