O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5.1. Basic class structure

Please attempt to write, compile, and execute HelloWorld.cs in the previous chapter [1] if you haven't already done so.

[1] See Chapter 4: Hello C#! on page 34.

Let's review HelloWorld.cs. Notice that it is structurally very similar to a Java class:

1: // HelloWorld.cs
2: public class TestClass{
3:   public static void Main (){
4:     System.Console.WriteLine("Hello C#! Here I come!");
5:   }
6: }

Like Java

  • The Main method, and all other methods, must be inside a class [2] (or a struct – the struct will be explained in Chapter 26).

    [2] In C/C++, the Main method is defined outside a class as a global method. Like Java, and unlike C/C++, C# does not support global methods or variables. Declarations of methods/variables must always ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required