Showing posts with label Tech. Show all posts
Showing posts with label Tech. Show all posts

Who Designed C#?

C# was developed by Microsoft, as part of the .NET initiative, around the year 2000.  The main designer and architect for the language was Anders Hjelsberg.

How is C# Different From Java?

C# and Java are both object-oriented languages that derive their syntax from C and run in a managed environment.  There are, however a number of differences.  Here are the main ones:
  • Syntactic differences, e.g. “class B extends A” instead of “class B : A”
  • Java doesn’t use namespaces
  • C# lock statement vs. Java synchronized statement
  • C# has a few more access modifiers than Java
  • In Java, enumerated types are full-fledged classes
  • C# allows strings in switch statements
  • C# programs make use of the .NET Framework; Java programs use the Java SE


Reading and Writing from the Console

To read and write from the console, use Console.Read, Console.ReadLine, Console.Write, Console.WriteLine.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
namespace ReadWriteConsole
{
    class Program
    {
        static void Main()
        {
            Console.Write("This ");
            Console.WriteLine("is all on one line.");
            Console.Write("Enter your name: ");
            string name = Console.ReadLine();
            Console.WriteLine(name);
            Console.Read();     // pause
        }
    }
}

The Main() Function Can Return an int

Instead of returning void, Main() can return an integer value, which can then be read by the calling program or script.  Notice that the return type of the function is an int


1
2
3
4
5
6
7
8
9
10
11
12
class Program
{
    static int Main()
    {
        Console.WriteLine("Doing something..");
 
        if (DateTime.Today.DayOfWeek == DayOfWeek.Monday)
            return -1;     // Monday is bad
        else
            return 0;
    }
}