Thread: plz help me understand this

  1. #1
    Registered User
    Join Date
    Feb 2011

    Question plz help me understand this

    I've recently shifted from C++ to C#. I'm now developing my first code in C# and wondering what the difference between a class, interface and a code file is, in C#.
    which one is the equivalence of a header file in C++?
    if there is an equivalence for header file, can I add the implementation of the functions defined or do I have to write just the prototypes in it?
    and I'd like to know which type of the files mentioned above acts like the .cpp files in C++?

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Hannover, Germany
    In C# there are no header files. Your cs files contain exactly one (best practice, can be more) type. You don't need forward declarations in headers in C#, so there is no need to have redundant code.

    Interfaces can be thought of as abstract base classes. They define... well... the interface of a class. What public members it has. The implementation is up to the implementing class. As C# classes can only have a single base class, Interfaces are neccessary to provide a way for one class to have multiple... well... interfaces.

    cs files act like cpp files. But with a smarter compiler, that doesn't need redundant code to make it work. All cs files will automagically know all other files in the project and their declarations.

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Feb 2011

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    The caveat is that CS types can't be named if their namespace hasn't been brought into the local scope with the using directive.

    If you have types under the following namespaces:

    And you declare

    You can't access the types inside without adding it to your using declarations too.

    namespace foo {
        class test {
            public baz.mytype var; // compiler error!
    Last edited by Mario F.; 07-06-2011 at 11:21 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I don't understand this.
    By neveslave in forum C Programming
    Replies: 21
    Last Post: 05-14-2010, 06:20 PM
  2. Can someone help me understand this plz?
    By Arex Bawrin in forum C Programming
    Replies: 5
    Last Post: 02-18-2010, 06:42 PM
  3. Don't understand (int *a, a) etc etc
    By phil128 in forum C Programming
    Replies: 21
    Last Post: 07-02-2008, 04:10 AM
  4. I don't understand...
    By Lorin_sz in forum C++ Programming
    Replies: 9
    Last Post: 06-28-2005, 02:57 AM
  5. help me understand this
    By dra in forum C++ Programming
    Replies: 6
    Last Post: 06-01-2005, 05:49 PM