Introduction of C#

Data types in Python programming language.

Introduction

C# is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java.
In January 1999, Dutch software engineer Anders Hejlsberg formed a team to develop C# as a complement to Microsoft's NET framework. Initially, C# was developed as C-Like Object Oriented Language (Cool). The actual name was changed to avert potential trademark issues. In January 2000, NET was released as C#. Its NET framework promotes multiple Web technologies.
The term is sometimes spelled as C Sharp or C-Sharp.
C# is designed to work with Microsoft's .Net platform. Microsoft's aim is to facilitate the exchange of information and services over the Web, and to enable developers to build highly portable applications. C# simplifies programming through its use of Extensible Markup Language (XML) and Simple Object Access Protocol (SOAP) which allow access to a programming object or method without requiring the programmer to write additional code for each step. Because programmers can build on existing code, rather than repeatedly duplicating it, C# is expected to make it faster and less expensive to get new products and services to market.
Microsoft is collaborating with ECMA, the international standards body, to create a standard for C#. International Standards Organization (ISO) recognition for C# would encourage other companies to develop their own versions of the language. Companies that are already using C# include Apex Software, Bunka Orient, Component Source, devSoft, FarPoint Technologies, LEAD Technologies, ProtoView, and Seagate Software.
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. Visual C# provides an advanced code editor, convenient user interface designers, integrated debugger, and many other tools to make it easier to develop applications based on the C# language and the .NET Framework.


C# Language

C# syntax simplifies many of the complexities of C++ and provides powerful features such as nullable value types, enumerations, delegates, lambda expressions and direct memory access, which are not found in Java. C# supports generic methods and types, which provide increased type safety and performance, and iterators, which enable implementers of collection classes to define custom iteration behaviors that are simple to use by client code. Language-Integrated Query (LINQ) expressions make the strongly-typed query a first-class language construct.
As an object-oriented language, C# supports the concepts of encapsulation, inheritance, and polymorphism. All variables and methods, including the Main method, the application's entry point, are encapsulated within class definitions. A class may inherit directly from one parent class, but it may implement any number of interfaces. Methods that override virtual methods in a parent class require the override keyword as a way to avoid accidental redefinition. In C#, a struct is like a lightweight class, it is a stack-allocated type that can implement interfaces but does not support inheritance.
In addition to these basic object-oriented principles, C# makes it easy to develop software components through several innovative language constructs, including the following:
Encapsulated method signatures called delegates, which enable type-safe event notifications.
Properties, which serve as accessors for private member variables.
Attributes, which provide declarative metadata about types at run time.
Inline XML documentation comments.
Language-Integrated Query (LINQ) which provides built-in query capabilities across a variety of data sources.

If you have to interact with other Windows software such as COM objects or native Win32 DLLs, you can do this in C# through a process called 'Interop.' Interop enables C# programs to do almost anything that a native C++ application can do. C# even supports pointers and the concept of unsafe code for those cases in which direct memory access is absolutely critical.
The C# build process is simple compared to C and C++ and more flexible than in Java. There are no separate header files, and no requirement that methods and types be declared in a particular order. A C# source file may define any number of classes, structs, interfaces, and events.


.NET Framework Platform Architecture

C# programs run on the .NET Framework, an integral component of Windows that includes a virtual execution system called the common language runtime (CLR) and a unified set of class libraries. The CLR is the commercial implementation by Microsoft of the common language infrastructure (CLI), an international standard that is the basis for creating execution and development environments in which languages and libraries work together seamlessly.
Source code written in C# is compiled into an intermediate language (IL) that conforms to the CLI specification. The IL code and resources, such as bitmaps and strings, are stored on disk in an executable file called an assembly, typically with an extension of .exe or .dll. An assembly contains a manifest that provides information about the assembly's types, version, culture, and security requirements.
When the C# program is executed, the assembly is loaded into the CLR, which might take various actions based on the information in the manifest. Then, if the security requirements are met, the CLR performs just in time (JIT) compilation to convert the IL code to native machine instructions. The CLR also provides other services related to automatic garbage collection, exception handling, and resource management. Code that is executed by the CLR is sometimes referred to as managed code, in contrast to unmanaged code which is compiled into native machine language that targets a specific system. The following diagram illustrates the compile-time and run-time relationships of C# source code files, the .NET Framework class libraries, assemblies, and the CLR.
Language interoperability is a key feature of the .NET Framework. Because the IL code produced by the C# compiler conforms to the Common Type Specification (CTS), IL code generated from C# can interact with code that was generated from the .NET versions of Visual Basic, Visual C++, or any of more than 20 other CTS -compliant languages. A single assembly may contain multiple modules written in different .NET languages, and the types can reference each other just as if they were written in the same language.
In addition to the run time services, the .NET Framework also includes an extensive library of over 4000 classes organized into namespaces that provide a wide variety of useful functionality for everything from file input and output to string manipulation to XML parsing, to Windows Forms controls. The typical C# application uses the .NET Framework class library extensively to handle common 'plumbing' chores.


Visual Studio

Visual Studio is a complete set of development tools for building ASP.NET Web applications, XML Web Services, desktop applications, and mobile applications. Visual Basic, Visual C#, and Visual C++ all use the same integrated development environment (IDE), which enables tool sharing and eases the creation of mixed-language solutions. In addition, these languages use the functionality of the .NET Framework, which provides access to key technologies that simplify the development of ASP Web applications and XML Web Services.

Visual Studio .NET mean?
Visual Studio .NET is a Microsoft-integrated development environment (IDE) that can be used for developing consoles, graphical user interfaces (GUIs), Windows Forms, Web services and Web applications. Visual Studio is used to write native code and managed code supported by Microsoft Windows, Windows Mobile, Windows CE, .NET Framework, .NET Compact Framework and Microsoft Silverlight. Visual Studio .NET's code editor supports IntelliSense and code refactoring, while the Visual Studio .NET integrated debugger supports both source and machine-level debugging. Visual Studio .NET includes other built-in tools, like a form designer, which is useful when building GUI applications, a Web designer that creates dynamic Web pages, a class designer that is used to create custom libraries, and a schema designer for database support.
To improve functionality at many levels, plug-ins may be added to provide support for source-control systems, while domain-specific language editors and visual designers are examples of new tool sets that may be added via these plug-ins.
Built-in language services offer support for different well-known languages (like C#, C++, VB, and F#), while language services may be installed separately to support languages, including M, Python, and Ruby, among other supported languages.


Compiler for C#

Using Microsoft Visual Studio 2008 version 9.0.21022.8 RTM
Install Visual Studio and Open Visual Studio, Goto File Click on New > and Select Project... then New Project Dialog Box opening in New Project Dialog Box in Project types: Click on Visual C#, and in Templetes: Click on Console Application if you giving name give it any name, if U not give name then it automatically using Default name, default name is (ConsoleApplication) and same process for Solution Name: and Press OK Button.
after pressing OK Button then it showing the following code on screen.

Comments in C#


//   is used for Single Line Comment
/*   is used for Multiple Line Comment  */
For Example:
//   This is Single Line Comment.
/*    And This
is Multiple Line
Comment.   */

C# Program

Line 1 using System; (known as a using directive) is generated by the Visual Studio IDE and declares that the program uses features in the System namespace, A namespace groups various C# features into related categories, One of the great strengths of C# is that C# programmers can use the rich set of namespaces provided by the .NET framework. These namespaces contain code that programmers can reuse, rather than reinventing thewheel. This makes programming easier and faster, The namespaces that are defined in the .NET Framework contain preexisting code known as the .NET Framework Class Library.
Lines 4–10 define our first class (these lines collectively are called a class definition), C# programs consist of pieces called classes, which are logical groupings of members (e.g., methods) that simplify program organization.
The left brace ({) at line 5 begins the body of the class definition, The corresponding right brace (}) at line 9 ends the class definition.
Line 6 is present in all C# console and Windows applications, These applications begin executing at Main, which is known as the entry point of the program, The parentheses after Main indicate that Main is a program building block, called a method.
The left brace ({) on line 7 begins the body of the method definition (the code which will be executed as a part of our program), A corresponding right brace (}) terminates the method definition's body (line 8).
line 1, 2, 4, and 6 must write in every program.
using System.Collections.Generic;, using System.Linq; and using System.Text; If you not write this three libraries then no problem it working normally it is Ur choice write or not, and If U not write using System; library the U need to write System. before write and read line like System.Console.WriteLine("Pakistan Zindabad!"); and for read line System.Console.ReadLine(); but I writing all libraries in every program.

C# Program Structure


  using System;  //using 'system' library, this library is must in every Program.
  using System.Collections.Generic; //using 'Generic' library.
  using System.Linq;  //using 'Linq' library.
  using System.Text;  //using 'Text' library.
  namespace ConsoleApplication //'ConsoleApplication' is the name of Project'namespace'.
  { //namespace starting brace.
      class Program //'Program' is the name of class If U changing it then change it.
      {            //class starting brace.
                   //The main entry point for the application.
          static void Main(string[] args) //this is main.
          {       //main body start brace.
       //TODO: Add code to start application here.
          }      //main body closing brace.
      }          //class closing(ending) brace.
   }            //namespace closing brace.
namespace

Namespaces are used to provide a named space in which your application resides. They're used especially to provide the C# compiler a context for all the named information in your program, such as variable names. Without namespaces, you wouldn't be able to make, e.g., a class named Console, as .NET already uses one in its System namespace. The purpose of namespaces is to solve this problem, and release thousands of names defined in the .NET Framework for your applications to use, along with making it so your application doesn't occupy names for other applications, if your application is intended to be used in conjunction with another. So namespaces exist to resolve ambiguities a compiler wouldn't otherwise be able to do.

Class

A class is a group of related methods and variables. A class describes these things, and in most cases, you create an instance of this class, now referred to as an object. On this object, you use the defined methods and variables. Of course, you can create as many instances of your class as you want to. Classes, and Object Oriented programming in general, is a huge topic.
A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events. A class is like a blueprint. It defines the data and behavior of a type. If the class is not declared as static, client code can use it by creating objects or instances which are assigned to a variable. The variable remains in memory until all references to it go out of scope. At that time, the CLR marks it as eligible for garbage collection. If the class is declared as static, then only one copy exists in memory and client code can only access it through the class itself, not an instance variable. For more information, see Static Classes and Static Class Members.
Unlike structs, classes support inheritance, a fundamental characteristic of object-oriented programming. Classes are declared by using the Class keyword.
The class keyword is preceded by the access level. Because public is used in this case, anyone can create objects from this class. The name of the class follows the class keyword. The remainder of the definition is the class body, where the behavior and data are defined. Fields, properties, methods, and events on a class are collectively referred to as class members.

static void Main(string[] args)

static: all member and variable can be accessed.
void: mean that no return value is result from method like showing or printing some thing.
Main: to refer to the entry point of your program to be compiled before executed when you click debug.
(): the input of method.
string: the type of your array return with string so you need to take it in your brain when you deal with variable.
[]: refer to an array.
args: the name of array which is input of method that return array of string ( brief of arguments).

Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity.
The Main method is the entry point of a C# console application or windows application. (Libraries and services do not require a Main method as an entry point.). When the application is started, the Main method is the first method that is invoked.
There can only be one entry point in a C# program. If you have more than one class that has a Main method, you must compile your program with the /main compiler option to specify which Main method to use as the entry point.
C# uses for multiple-line comments(/*…*/) and uses for single-line comments (//).
The Console class enables programs to output information to the computer's standard output. Class Console provides methods that allow C# programs to display strings and other types of information in the Windows command prompt.
Method Console.WriteLine displays (or prints) a line of text in the console window, When Console.WriteLine completes its task, it positions the output cursor (the location where the next character will be displayed) at the beginning of the next line in the console window. (This is similar to pressing the Enter key when typing in a text editor—the cursor is repositioned at the beginning of the next line in the file.) The entire line, including Console.WriteLine, its argument in the parentheses ("Pakistan Zindabad!") and the semicolon ;, is called a statement.
Every statement must end with a semicolon (known as the statement terminator). When this statement executes, it displays the message Pakistan Zindabad! in the console window.
In C# statements we normally precede each class name with its namespace name and a period, For example, line would normally be System.Console.WriteLine("Pakistan Zindabad!");
for the program to run correctly, The using directive on line (using System) eliminates the need to specify explicitly the namespace System when using classes in the namespace. This can save time and confusion for programmers.
Selecting Debug > Start Without Debugging Ctrl+F5 causes the command window to prompt the user to press a key after the program terminates, allowing the user to observe the program,s output, In contrast, if we run this program using Debug -> Start Debugging F5, as we did for the Windows application, a command window opens, the program displays the message Pakistan Zindabad!, then the command window closes immediately.