C#


INTRODUCTION

C# tutorial provides basic and advanced concepts of C#. C# tutorial is designed for beginners and professionals. C# is a programming language of .Net Framework.

C# tutorial includes all topics of C# such as first example,

control statements, objects and classes, inheritance, constructor, destructor, this, static, sealed, polymorphism, abstraction, abstract class, interface, namespace, encapsulation, properties, indexer, arrays, strings, sregex, exception handling, multithreading, File IO, Collections etc.

PREREQUISITE

before learning C#, you must have the basic knowledge of C Programming Language.

WHAT IS C#?


C# is pronounced as “C-Sharp”. It is an object-oriented programming language provided by Microsoft that runs on .Net Framework.

By the help of C# programming language, we can develop different types of secured and robust applications:


  • Window applications
  • Web applications
  • Distributed applications
  • Web service applications
  • Database applications etc.

C# is approved as a standard by ECMA and ISO. C# is designed for CLI (Common Language Infrastructure). CLI is a specification that describes executable code and runtime environment.

C# programming language is influenced by C++, Java, Eiffel, Modula-3, Pascal etc. languages.

There are many differences and similarities between C++ programming language and C#. A list of top differences between C++ and C# are given below:

NoC++C#
1)C++ is a general purpose, case-sensitive, free-form programming language that supports object-oriented, procedural and generic programming.C# is pronounced as “C-Sharp”. It is an object-oriented programming language provided by Microsoft that runs on .Net Framework.
2)In C++, multiple inheritance is possible through class.In C#, multiple inheritance is not possible through class.
3)In C++, memory management is handled manually.In C#, memory management is handled automatically
4)In C++, pointers can be used anywhere in a program.In C#, pointers can be used only in unsafe mode.
5)C++ programming is based on OOPs concept.C# programming is based on Component and OOPs concept.
6)C++ is a programming language that runs on all platforms.C# is a programming language that rarely used outside Windows.
7)C++ programming can be used to create console applications.C# programming can be used to create console applications, Windows applications, Mobile applications, etc.

C# HELLO WORLD

In C# programming language, a simple “hello world” program can be written by multiple ways. Let’s see the top 4 ways to create a simple C# example:

  • Simple Example
  • Using System
  • Using public modifier
  • Using namespace

C# SIMPLE EXAMPLE

	
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
}
}

Output:

	
Hello World!

DESCRIPTION

class: is a keyword which is used to define class.

Program: is the class name. A class is a blueprint or template from which objects are created. It can have data members and methods. Here, it has only Main method.

static: is a keyword which means object is not required to access static members. So it saves memory.

void: is the return type of the method. It does’t return any value. In such case, return statement is not required.

Main: is the method name. It is the entry point for any C# program. Whenever we run the C# program, Main() method is invoked first before any other method. It represents start up of the program.

string[] args: is used for command line arguments in C#. While running the C# program, we can pass values. These values are known as arguments which we can use in the program.

System.Console.WriteLine(“Hello World!”): Here, System is the namespace. Console is the class defined in System namespace. The WriteLine() is the static method of Console class which is used to write the text on the console.



C# EXAMPLE: USING SYSTEM

If we write using System before the class, it means we don’t need to specify System namespace for accessing any class of this namespace. Here, we are using Console class without specifying System.Console.

	
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}

Output:

	
Hello World!

C# EXAMPLE: USING NAMESPACE

We can create classes inside the namespace. It is used to group related classes. It is used to categorize classes so that it can be easy to maintain.

	
using System;
namespace ConsoleApplication1
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

output

	
Hello World!

VARIABLE


A variable is a name of memory location. It is used to store data. Its value can be changed and it can be reused many times.

It is a way to represent memory location through symbol so that it can be easily identified.

The basic variable type available in C# can be categorized as:

Variable TypeExample
Decimal typesdecimal
Integral typesint, char, byte, short, long
Boolean typesTrue or false value, as assigned
Floating point typesfloat and double
Nullable typesNullable data types

the syntax to declare a variable:

		
type variable_list;

The example of declaration and initialization variable is given below:

		
int i=2,j=4; //declaring 2 variable of integer type
float f=40.2;
char ch='B';

RULES FOR DEFINING VARIABLES

A variable can have alphabets, digits and underscore.

A variable name can start with alphabet and underscore only. It can’t start with digit.

No white space is allowed within variable name.

A variable name must not be any reserved word or keyword e.g. char, float etc.

Leave a comment