Wednesday, January 22, 2014

DoubleVariable.cs

using System;

class DoubleVariable
    {
        static void Main(string[] args)
        {
            int i;
            double d;
            i = 100 / 3;
            d = 100 / 3;  
            Console.WriteLine("i is {0}",i);
            Console.WriteLine("d is {0}",d);
            d = 100 / 3.0;
            Console.WriteLine("now d is {0}", d);
        }
    }

Program Notes:
 d = 100 /3 ; stores 33 in variable 'd' since the expression int / int results in int.
when it is re-written as d = 100 / 3.0; now int / double results in double

IntDemo.cs

using System;class IntDemo
    {
        static void Main(string[] args)
        {
            int year = 2014;
            Console.WriteLine("Happy New Year {0}", year);
        }
    }
Point to note : In languages like C and Java int is a primitive type. In C# it is a struct type which has
more members. While typing or copying this program in Visual Studio, keep the cursor over the keyword 'int' and press F12 to explore.

ConsoleExample.cs


using System;
class ConsoleExample
{      
  static void Main()       
  {          
          Console.Write("Dear Friend, ");
          Console.WriteLine("Welcome to C-Sharp Programming");       
  }
 }


// Console.Write() displays the output and cursor stays in the same line
// Console.WriteLine() displays the output and cursor moves to the next line
// This is something similar to "\n" is C language

UsingExample.cs

using System;

class UsingExample {

  static void Main() {
    Console.WriteLine("example for  'using' keyword");

  }
}

// since using System; is added in the beginning of the program
// no need to include it again with Console.WriteLine()

Thursday, January 16, 2014

HelloWorld.cs


class HelloWorld
{
      static void Main()
       {
            System.Console.WriteLine("Welcome to C#");
       }
}

C# command-line compiler

To create and run programs using the C# command-line compiler, follow these three steps:
1. Enter the program using a text editor.
2. Compile the program using csc.exe.
3. Run the program.

- source C# 4.0 complete Reference by Herbert Schildt

The name of csharp compiler is csc.exe which can be located in .Net framework folder.


Points to Remember (compiled from various sources)

  •   In languages like C and Java int is a primitive type. In C# it is a struct type which has more members. While typing a program in Visual Studio, keep the cursor over the word 'int' and press F12 to explore.
  • The MSIL code is actually stored in an .exe file, but this file does not contain executable code. It contains the information needed by the JIT to execute the code when you run it.