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.

The .Net Framework

Central to the .NET platform is a development environment known as the .NET Framework. The framework provides a lot of features, but for now all you need to know is that the C# language provides you with the elements that allow you to access the framework to make your programs work. You will learn about these elements later.

Introduction to C#

C# is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework. You can use C# to create Windows client applications, XML Web services, distributed components, client-server applications, database applications, and much, much more.

C# syntax is highly expressive, yet it is also simple and easy to learn. The curly-brace syntax of C# will be instantly recognizable to anyone familiar with C, C++ or Java. Developers who know any of these languages are typically able to begin to work productively in C# within a very short time. C# syntax simplifies many of the complexities of C++ and provides powerful features.

 - source MSDN


The lead architect of the team developing the language C# is Anders Hejlsberg


Our First C# Program

// HelloWorld.cs

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



Our first C# program
*****************
All C# programs begins with a call to the method Main(). This happens automatically when you execute the program.

using System; does the inclusion of System namespace and in turn all necessary classes for use in this program.

You will be including relevant namespaces based on the program needs in future.

Console is a class. WriteLine is a method in Console class which does the output operation.

Without including namespace System, the program is re-written as follows

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

Welcome Note

Introduction

I have been using many programming languages since 1992 and constantly evaluating new programming languages for its usefulness. I don't worry about the complexity involved in learning, but I choose to learn based on the utilization of the language in real-world applications.

15 years back if you want to develop a desktop application, you will be probably choosing Visual Basic and ASP or PHP for web development. I was thinking how wonderful it would be if there is one language which can be used for any kind of application development, be it desktop app, web app or even mobile.

I found one. It is C# (pronounced as C-Sharp). A language which is going to stay here  forever, like its ancestor 'C' .

In this blog I am going to explore C# through working examples.

Happy Learning and wish you all success in your learning efforts.

Padmanaban K
https://www.facebook.com/pad710