Hello Students in this blog from C and C++ Course in Pune, I am discussing the information about c#(C sharp) Programming Language.
We have seen the number of programming Languages Like c, CPP, java just like that C# is also an object-oriented programming language developed by Microsoft that runs only on the Dot Net framework
Used to develop different types of applications like desktop Application, web Application, Windows Application, console Application, any other type of application using Visual Studio.
Using c# we can develop secure and robust applications.
Now Let’s understand what is C# with the help of C and C++ Classes in Pune?
C# is a Simple, powerful, object-oriented Programming Language which is runs on the dot net framework only. To create C# programs we can use visual studio.
C# is mainly influenced by C++.
Anders Hejlsberg is the father of C# Language. Currently, C# 8.0 is running.
Before proceeding the further information of c# Lets understand the Dot net framework
Dot Net framework
Dot net Framework is a Collection of Libraries that help us to develop different kinds of applications like a web application, windows Applications, Mobile applications. Dot net framework provides a wide range of features and support for any type of Application.
Dot net framework is designed and developed by Microsoft, It was first released in 2000.
The framework contains a Large number of Libraries, which are commonly known as Framework Class Library (FCL) also known as Base class Library.
To execute the programs which are written in Dot net is called Common Language Runtime (CLR)
CLR, FCL are essential features of Dot Net Framework.
Dot Net Support more than 60 different programming languages. Like
C#, F#,VB.net, J#, VC++, python, Jscript, Pascal, etc.
As a dot net programmer, you need not learn all Programming Languages. If you Learn only one Language with the help of C and C++ Training in Pune that is enough to create Applications.
Apart from Language support Dot net provides the following services like
Memory Management,
networking,
security,
Type-safety.
These are Some of the Common Services.
Here is a diagram of the Dot Net Framework.
Link Address is
https://static.javatpoint.com/csharp/net/images/net-framework.png
CLR(Common Language Runtime)
The main heart of Dot net is CLR(Common Language Runtime).CLR is the main execution engine. It Converts the program into machine Code.CLR act as the interface between the Dot net framework and Operating System.
The task of CLR is
Exception Handling
Memory Management
Garbage Collection
Security
Type-Safety
Portability
Interoperability
Here is a Diagram of CLR
The link Address is:
https://static.javatpoint.com/csharp/net/images/net-common-language-runtime.png.
FrameWork Class Library(FCL)
FCL is a Collection of Classes to build any type of Application. Also known as Base Class Library.
Here is a diagram of FCL
Link Address is
https://static.javatpoint.com/csharp/net/images/net-framework-base-class-library.png
Lets us Move to C#.
C#1.0 was introduced in 2002 with Some of the basic features Now in c# 8.0. It has made so many major changes added different types of libraries. To create a fast and secure and robust application.
Features of C#
C# provides several features that are mentioned below.
C# is an object-oriented Programming Language.
Rich in Library
Scalable, Powerful, Robust, Type safety, fast
Modern Programming Language.
Link Address
https://static.javatpoint.com/csharp/images/csharp-features1.png
Compared to other programming Language Compilation and execution time of c# is Fast. So we can choose it for development.
Let’s Understand Syntax
C# Language Syntax Looks like java only. C# is mainly influenced by CPP.
using System;
namespace ConsoleApp1
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine(“Hello World!”);
}
}
}
Description of the Above Program.
Using is a keyword. The system is the namespace. the namespace contains a group of classes. a namespace is used to organize programs for easy access.
Public: It is Access Specifier.
Class is a keyword used to declare the class. Class a blueprint for creating objects.
Static: it is a keyword this helps to access the main method without the object. So it saves memory.
Main() : it is entry point of program . where C# Compiler starts from this point.
string[] args: this is a Command-Line argument that is passed from the operating system.
Console.WriteLine: From Console Class, the Write Line method is used to display output on Screen.
Variables and Data types in C#
C# is a strongly typed language. So we need to specify a data type for the variable. You can not assign one value to another value. In C# variables are divided into two types based on memory and how the variables store the value.
Value Type and, Reference Type.
Value Type: In value type data and variables holds the data in their own memory space.
The followings are value Type
int, char, float, double, enum, long, short, byte, etc.
Reference Type: Reference type does not store its value directly. But it stores the address of the value. In reference Type, it always contains the pointer to another memory location.
The followings are reference Types.
Strings, Arrays, Class, Delegate
We can Declare Variables with the following Syntax
Data type variable_name;
the data type can be value type for reference Type
Here is a Program for variables
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Demo of variables”);
int x = 100;
double y = 89.90;
String name = “student”;
Console.WriteLine(x);
Console.WriteLine(y);
Console.WriteLine(name);
}
}
In c# predefined data types are called alias to their .Net Types (CLR) name.
Every data type has a default value like all Numeric data Types Weather you declare int, long, byte, short have 0 value.
And boolean has a false value String has a null value. And char has ‘\0’.
In object-oriented Programming Variables’ can be divided into three Types like
Local variable
Instances variable
Static variable
All instance variables will have default values. The variable which is declared in a class is called instance variables. You can access the instance variable with the help of an object only.
The variables which are declared in a method or function is known as a local variable. The scope of the local variable remains inside a function only.
The variables which are declared using static keyword is known as a static variable. Static variables are part of the class So Accessing static variables can be done with the class name and not with a variable name. Static variables belong to a class. Static variables are sharable to all objects. Any data that is sharable to all objects is static a variable. The main use of static variables is for memory management.
In c# 7.1 every data type default value. The values of certain data types are automatically converted into other data types in c#. this is called an implicit conversion.
For example, an int can be converted into afloat.
Dynamic Data Type in C#
Apart from these built-in variables C# also supports Dynamic Data Types.
These Dynamic Data Types are var, Object, Dynamic.
The Dynamic Data Type Concept is introduced in c#(4.0) and dot net framework (4.5). In Dynamic Data Type without declaring the dataType of the variable, you can store any value that value will be checked at run time rather than compile time.
Dynamic. This is a new data type introduced in c# 4.0. This dynamic will escape the type checking at compile time. The variable type will be checked at run time only. The dynamic variable will be declared by the dynamic keyword.
Example of Dynamic Keyword
class Program
{
static void Main(string[] args)
{
dynamic d=100;
Console.WriteLine(d);
d=”hello world”;
Console.WriteLine(d);
d=true;
Console.WriteLine(d);
}
}
After compiling and running the program we will get output based on the value of the variable.
dynamic types changes at run time based on assigned value.
The compiler can convert any object into a dynamic type implicitly. This is a boon for developers. So developers can easily switch between dynamic and non-dynamic types.
The Dynamic Language run time API will provide support for a dynamic data type.
A mostly dynamic data type is used in API Programming Like COM APIs like office automation API. In C# dynamic keyword implementation can be done in IronPython, IronRuby Languages.
Var: var can be used to declare any built-in data type or user-defined data type or anonymous type. C# Compiler implicitly converts based on the data type. It is introduced in c# 3.0.
This is used mostly in Lambda Expressions.
Example of var keyword
Apart from These Data Type. C# also supports Dynamic Types are var, Dynamic, Object.
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var i = 10;
Console.WriteLine(“Type of i is {0}”, i.GetType());
var str = “Hello World!!”;
Console.WriteLine(“Type of str is {0}”, str.GetType());
var dbl = 100.50d;
Console.WriteLine(“Type of dbl is {0}”, dbl.GetType());
var isValid = true;
Console.WriteLine(“Type of isValid is {0}”, isValid.GetType());
var arr = new[] { 1, 10, 20, 30 };
Console.WriteLine(“Type of arr is {0}”, arr.GetType());
Console.Read();
}
}
}
Rules of var keyword
var is known as an implicitly typed local variable. You need to assign value to Var otherwise Compiler will give a compiler error.
var x;
x=100;
This line will give compilation error. variable must be initialized immediately.
Multiple declarations of var are not allowed.
var i=100,j=200,k=300 ;
Single line declaration is not allowed.
Var cannot be used as a function parameter.
void m1(var x) //compile time error
{
Console.writeln(x);
}
Uses of var keyword
1) Var can be used in for loop and foreach loop.
2) Var can be used in LINQ queries.