C# - Overview
C# is a modern, general-purpose, object-oriented programming language developed
by Microsoft and approved by European Computer Manufacturers Association (ECMA)
and International Standards Organization during the development of .Net Framework
(ISO).C# was developed by Anders Hejlsberg and his team.
C# is designed for Common Language Infrastructure (CLI), which consists of
high-level languages on different computer platforms and architectures. The executable
code and runtime environment that allows use of various
The following reasons make C# a widely used professional language.
It is a modern, general-purpose programming language
It is object oriented.
It is component oriented.
It is easy to learn.
It is a structured language.
It produces efficient programs.
It can be compiled on a variety of computer platforms.
It is a part of .Net Framework.
Sample C# Program :
Sample C# Program :
using System;
using System.IO;
namespace HelloWorldApp
{
class Hello
{
static void Main(string[] args)
{
Console.WriteLine("Hello!!!");
Console.ReadKey();
}
}
}
Let us look at the various parts of the given program:
- The first line of the program using System; - the using keyword is used to include the System namespace in the program. A program generally has multiple using statements.
- The next line has the namespace declaration. A namespace is a collection of classes.
- The HelloWorldApplicationnamespace contains the class HelloWorld.The next line has a class declaration, the class HelloWorld contains the data and method definitions that your program uses. Classes generally contain multiple methods. Methods define the behavior of the class. However, the HelloWorld class has only one method Main.
- The next line defines the Main method, which is the entry point for all C# programs. The Main method states what the class does when executed.
- The next line /*...*/ is ignored by the compiler and it is put to add comments in the program.
- The Main method specifies its behavior with the statement Console.WriteLine("Hello World");
- WriteLine is a method of the Console class defined in the System namespace. This statement causes the message "Hello, World!" to be displayed on the screen. The last line Console.ReadKey(); is for the VS.NET Users. This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET. The .Net Framework.
- The .Net framework is a revolutionary platform that helps you to write the following types of applications:
- Windows applications
- Web applications
- Web services
- Common Language Runtime (CLR)
- The .Net Framework Class Library
- Common Language Specification
- Common Type System
- Metadata and Assemblies
- Windows Forms
- ASP.Net and ASP.Net AJAX
- ADO.Net
- Windows Workflow Foundation (WF)
- Windows Presentation Foundation
The using Keyword
The first statement in any C# program is using System; The using keyword is used for including the namespaces in the program. A program can include multiple using statements.The class Keyword
The class keyword is used for declaring a class.Eg. public class class_Name
Variables
The variables in C#, are categorized into the following types, The variables in C#, are categorized into the following types:- Value types
- Reference types
- Pointer types
Value Type
Value type variables can be assigned a value directly. They are derived from the class System.ValueType The value types directly contain data. Some examples are int, char, and float, which stores numbers, alphabets, and floating point numbers, respectively. When you declare an int type, the system allocates memory to store the value. To get the exact size of a type or a variable on a particular platform, you can use the sizeof method. The expression sizeof(type) yields the storage size of the object or type in bytes. Following is an example to get the size of int type on any machine:
Example
using System;
using System.IO;
namespace DataTypeApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Size of int: {0}", sizeof(int));
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following
result:
Size of int: 4
Reference Type
The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables. In other words, they refer to a memory location. Using multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value. Example of built-in reference types are: object, dynamic, and string.Object Type
The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). Object is an alias for System.Object class. The object types can be assigned values of any other types, value types, reference types, predefined or user-defined types. However, before assigning values, it needs type conversion. When a value type is converted to object type, it is called boxing and on the other hand, when an object type is converted to a value type, it is called unboxing.object obj; obj =100 // this is boxing
Dynamic Type
You can store any type of value in the dynamic data type variable. Type checking for these types of variables takes place at run-time. Syntax for declaring a dynamic type is:dynamicDynamic types are similar to object types except that type checking for object type variables takes place at compile time, whereas that for the dynamic type variables takes place at run time.= value; For example, dynamic d =20
String Type
The String Type allows you to assign any string values to a variable. The string type is an alias for the System.String class. It is derived from object type. The value for a string type can be assigned using string literals in two forms: quoted and @quoted.For example, String str = "cSharpTutorial; A @quoted string literal looks as follows: @"cSharpTutorial; The user-defined reference types are: class, interface, or delegate.