C++ Classes Vs. C# Classes - How have classes changed?
(Page 2 of 6 )
As you may well know, C# is based partly on C++ and partly on the Java language syntax. The rest of the C# language syntax was created totally from scratch, giving it a more modern design.
This is noticeable as soon as you begin to create classes with C#. Let's start of by looking at how we can create and instantiate a very simple class with both C++ and C#:
C++ Version:
#include <iostream>
class MyClass
{
public: void doSomething()
{
std::cout << "This is some text";
}
};
void main()
{
MyClass mc;
mc.doSomething();
}
C# Version:
using System;
class MyClass
{
public void doSomething()
{
Console.WriteLine("This is some text");
}
}
class EntryPoint
{
public static void Main()
{
MyClass mc = new MyClass();
mc.doSomething();
}
}
There are a couple of differences between these two snippets of code. Firstly, our C++ example uses the #include directive to physically include the contents of the iostream.h library. The C# code snippet contains a using directive which tells the C# compiler that we would like access to all of the other namespaces and classes contained within the System namespace. Using directives are not the same as C++ #include directives: using directives only tell the C# compiler the name of the namespace we're after (in this case, the level one namespace, system), and don't physically include any other files into our C# application.
Secondly, in C#, the main function is now declared as Main (note the capital M).
Thirdly, a C++ class declaration always has a semi-colon after its last curly bracket. In C#, this semi-colon is optional and is usually left out.
Fourthly, you'll notice that in C#, we have to explicitly append the access specifier to each of our method and member declarations. If we don't, then they are assumed to be private (accessible only by the instance of the class in which they were created) just like in C++. In C#, there are five access specifiers that we can specify to denote how our class members and methods can be used:
- public: Accessible by any other class
- private: Accessible only by the class in which it is declared
- protected: Accessible only by the class in which it is declared, as well as any derived classes
- internal: Accessible only from within the same assembly (in C#, an assembly is a package of inter-related data that contains both code and meta data)
- protected internal: Accessible only by the class in which it is declared, as well as any derived classes in the same source code file
Lastly, just like in Java, C# methods can also be declared as static. This works in the same way as both C#'s and C++'s static keyword for variables. In C#, we can create and call a static method of a class like this:
using System;
class MyClass
{
public static void doSomething()
{
Console.WriteLine("This is some text");
}
};
class EntryPoint
{
public static void Main()
{
MyClass.doSomething();
}
}
Notice how we refer directly to the class declaration and not an instantiation? This is an extremely handy addition to the C# language, and saves us both time and memory resources (i.e. We don't have to instantiate a class just to call one of its generic methods).Next: Limiting access to classes with class modifiers >>
More C# Articles
More By Jordan Leverington