Lesson 1: Introduction to CSharp Programming Language






Lesson 1: Introduction to C# Programming Language

In introduction to C# programming language, we will begin our tutorials with one of the most widely used programming languages in today’s word.C# or C-Sharp, is a true objected oriented programming with clear syntax, better type casting, efficient use of memory through pointers (the pointers are only needed when we have to write a super efficient and safe access of memory code), garbage collection, the code is well organized through brackets makes it more professional.If you want to learn Visual Studio in detail first,then check this link because our whole development is based upon this fantastically architected platform,one could wish to develop everything with it.
In industries where Java, C++ and C# are in action; a C# developer will find some similarities between the coding format which will ease their switching (if required)
Moreover, C# is also available in Xamarin Studio for Android development………My choice of development environment has always been Visual Studio because it is easy to install, lots of help available, best intelligence, and all the languages which it support are converted into same computer readable language.(Further how to’s about Visual Studio features and installation will be uploaded in presentations on my shared drive for free download )

Lesson 1 (Introduction,Variables,Type Casting)Introduction

Approach 1

Open up your Visual Studio (VS 2010 is in my mind) with
administrative privileges.
Click Files—–>New—–>Project——->Console Application. in the
left pane, select C# option Name it as you wish and save it in a particular
directory. I will share the whole documents regarding the installation and
explaining the other features of Visual Studio IDE in my download link which is
absolutely virus free and the material there is free to download with pictorial
representations.

Approach 2

Another quick and easy approach is to use an online C# compiler, remove everything from  the code editor  and paste the complete  code there and then compile and execute by pressing the “Play” button at the top.

The most reliable and on line C# compilers can be found here

1. Compile Online (Press “Play” symbol button at the top)

2. IDE One (Press “Run” Button in green at the bottom)

You will see an already build method Main () when you are using Visual Studio IDE. The Main () method is considered as the heart of the C# programming. In each and every console (Command Line) or form based (GUI ) C# application. The Main () method always executes first. So be careful about tricking with it. There are not more than one  Main ()  method in normal practice. However an application can have two or more  Main ()  methods but we have to specify the  entry point  of the that Main ()  method by which our application starts its working

The code is:

using System;

namespace ConsoleApplication1

{

class Program

{
static void Main(string[] args)

{

Console.WriteLine(“Howdy”);

}

}

}

Output

Howdy!

When we look at the code of our very first program; we will see that the very beginning starts with the keyword using. This word indicates that we are including namespaces which include classes needed

For a program to function;both classes and namespaces will be discussed later.

The Main() method has a  static keyword  before the word void. It means that the method can be called without calling the instance of the class to which the method belongs. In simple, we use static  here to call this method with an ease. Instance  and static  will be discussed in details later. The void  before the method indicates that the method returns no value. Void means no type specified.

The brackets of Main () method contains arguments/parametersArguments/Parameters are the values passed into the method. Right now they can be considered command line input (s). The {} are the opening and closing parenthesis and they represent blocks of code for a particular method, class, property, interface, structures, interfaces, loops etc.

It is actually the boundary of the method Main() logic.

The Console is a class in the System namespace containing the WriteLine () method .The WriteLine () method displays the output. Another method of same behavior is Write (). The difference is ; Write () method prints everything in the same line and the other one prints every new printable argument on it in a new line whenever it is called.

Variables:

The name ‘variable’ is self explanatory in its meaning. ‘Vary’ means something which changes, alters or can be changed. So this clearly explains what is variable?
A variable can be called as a container of a value which may change when required. I used the word ‘may’ to indicate that most of the times we may need an unchanged variable as well as the variable which changes its value according to the need of a program.

Now fasten your seat belts, we will try to cover a whole lot of information about a variable in a single round.

Examples
int i; // declaring variable;data type is integer

i=1; // initializing a variable

float j=2; // both declaring and initialization with type float

var k=1; // var is often used when it is left on to the compiler to decide the type in execution

The double slash ‘//’   used in the examples is the single line comment.It is used to explain the logic of the line of a program.It doesn’t compile an often neglected

/ * comments denotion carry multiple lines

int,float,string,char etc, are all explicit types,whereas var is an inferred type

/* A quick brown fox jumps over the lazy dog.

Slow and steady wins the race */

/*<summary> This is the summary of the operations performed by the program.

The variables used in it etc </summary>*/

 

Local variables

The variables of any data type , which exist only within their specific container such as class, structure, method etc; are called Local Variables. These types of variables are invisible to the outside or un-inherited objects

Scope

The usefulness of an object within a block of code is known as its ‘scope’.

Example

using the System;

class MyClass

{

static void Main ()

{

/* variable a and its value 1 is only limited within the boundaries

On Main () function*/

int a=1;

Console.Write(a);

}

}

The type of a variable tells the compiler about the category in which the variable belongs, in order to perform operations.

int i=1 tells the compiler that the type of the variable is an integer and all the arithmetic operations are available for the variable such as addition, multiplication, subtraction, division etc.

Chart of commonly used Data Types
There is another type named as object. The object data type is capable of storing any type but one thing to be noted that if we want to perform operations properly; then the value of the object variables should have the same type.

Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{static void Main(string[] args)
{object o1 = 1;
object o2 = 2;int i1=(int)o1; // typecasted the object to int.Typecasting refers to converting the type of variables to the desired types

int i2 = (int)o2;

Console.WriteLine(“o1*o2={0}”,o1*o2); // design time error prevents running of the program because of the C# feature as a strongly typed language//
Console.WriteLine(“i1*i2={0}”,i1*i2); /* Output = i1*i2=2;

}

}
}

Type Casting:

Examplesint a = (int)1.6; // Output=1;

double d = 1.111;
char a = (char)d; //* Outputs the ASCII value of 1.111

int v1 = 10;
object v2 = v1; // Explicit casting.

int v3 = (int)v2; //Implicit casting

string cookie= “marie”;
object obj = cookie;
string temp = obj as string; // ‘As’ used to cast the value and store it in the variable

Convert, Parse and TryParse

Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.


Syed Adeel Ahmed on FacebookSyed Adeel Ahmed on GoogleSyed Adeel Ahmed on InstagramSyed Adeel Ahmed on Twitter

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.