• Welcome to Valhalla Legends Archive.
 

C# & C++ Question

Started by Mephisto, December 07, 2004, 08:16 PM

Previous topic - Next topic

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?

MyndFyre

Quote from: Mephisto on December 07, 2004, 08:16 PM
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?

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:

[StructLayout(LayoutKind.Explicit)]
private struct LGINT_HELPER
{
  [FieldOffset(0)]
  uint low_dword;
  [FieldOffset(4)]
  int high_dword;
  [FieldOffset(0)]
  long qword;
}

which lets you lay out these things in memory).
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

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?

MyndFyre

Quote from: Mephisto on December 07, 2004, 09:22 PM
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?

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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Kp

Quote from: Mephisto on December 07, 2004, 09:22 PMAlso, 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?

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?
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

MyndFyre

Quote from: Kp on December 08, 2004, 02:42 PM
Myndfyre, is such a feature available or is it required that every single declaration remind the compiler what protection it has?

You can declare multiple fields of the same type:


private int n1, n2, n3;


But you don't need to mark Class members as private; they are by default (like in C++).


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


Change it to a struct though:

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

Unlike C++, Struct fields are ALSO private by default.

This behavior is to enforce effective encapsulation practices.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Eibro

So what is the difference between a struct and a class in C#?
Eibro of Yeti Lovers.

Adron

Quote from: Mephisto on December 07, 2004, 09:22 PM
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?

Umm... Didn't you say you knew C++ ?

Mephisto

Yes, but I've never seen public class Something in C++.  If you can do that I sure didn't know about it.

MyndFyre

Quote from: Eibro[yL] on December 08, 2004, 05:29 PM
So what is the difference between a struct and a class in C#?
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 from: Mephisto on December 08, 2004, 05:59 PM
Yes, but I've never seen public class Something in C++.  If you can do that I sure didn't know about it.
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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

R.a.B.B.i.T

Quote from: Mephisto on December 08, 2004, 05:59 PM
Yes, but I've never seen public class Something in C++.  If you can do that I sure didn't know about it.
class ClassName {
    public:
        Bob();        // constructor
        ~Bob();        // destructor
        DoStuff(char * things[]);        // just some function
    private:
        DoSecretStuff(char * things[]);        // secretive stuff!
};
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 :)

Newby

Quote from: R.a.B.B.i.T on December 08, 2004, 07:54 PM
Quote from: Mephisto on December 08, 2004, 05:59 PM
Yes, but I've never seen public class Something in C++.  If you can do that I sure didn't know about it.
class ClassName {
    public:
        Bob();        // constructor
        ~Bob();        // destructor
        DoStuff(char * things[]);        // just some function
    private:
        DoSecretStuff(char * things[]);        // secretive stuff!
};
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 :)
He means something like

public class ClassName
{
    ...
};
- Newby

Quote[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

Quote<TehUser> Man, I can't get Xorg to work properly.  This sucks.
<torque> you should probably kill yourself
<TehUser> I think I will.  Thanks, torque.

R.a.B.B.i.T

Ahhh, I misinterpreted 90% of the discussion then..

Adron

Quote from: MyndFyre on December 08, 2004, 07:01 PM
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.

What about C++:

class sometoplevelclass {
   public: class publicclass {
     int bla;
   } instance1;
   private: class privateclass {
     int ble;
   } instance2;
   protected: class protectedclass {
     int blu;
   } instance3;
};

Kp

Quote from: MyndFyre on December 08, 2004, 05:12 PM
Quote from: Kp on December 08, 2004, 02:42 PM
Myndfyre, is such a feature available or is it required that every single declaration remind the compiler what protection it has?

You can declare multiple fields of the same type:


private int n1, n2, n3;

That's not quite what I meant.  I was wanting something that is the C# equivalent of:

class A {
     public:
        int X;
        float y;
        char z;
}; // all members of this class are globally accessible
but without needing to repeat public on every single variable.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!