Introduction to c#

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 is highly expressive, yet it is also simple and easy to learn. The curly-brace syntax of C# will be instantly recognizable to anyone familiar with C, C++ or Java. Developers who know any of these languages are typically able to begin to work productively in C# within a very short time. 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.

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.

From C# source code to machine execution

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.

hello world program in C#

  1. Start Visual Studio.
  2. On the menu bar, choose File, New, Project.
  3. The New Project dialog box opens.
  4. Expand Installed, expand Templates, expand Visual C#, and then choose Console Application.
  5. In the Name box, specify a name for your project, and then choose the OK button.
  6. The new project appears in Solution Explorer.
  7. If Program.cs isn't open in the Code Editor, open the shortcut menu for Program.cs in Solution Explorer, and then choose View Code.

Program


//C# hello world program
using System;

namespace Hello_World
{
    class Hello
    {
        static void Main()
        {
            Console.WriteLine("Hello World");
            //keep the console window open
            Console.ReadKey();
        }
    }
}
  
  

Output


Hello World
  

Explanation

The first line contains a comment. The characters // convert the rest of the line to a comment.

//C# hello world program
  
You can also comment out a block of text by enclosing it between the /* and */ characters.

Main method

A C# console application must contain a Main method, in which control starts and ends. The Main method is where you create objects and execute other methods. The Main method is a static method that resides inside a class or a struct. In the previous "Hello World" example, it resides in a class named Hello. You can declare the Main method in one of the following ways: 1.It can return void.

static void Main()
{
    //...
}
  
2.It can also return an integer.

static int Main()
{
    //...
    return 0;
}
  
3.With either of the return types, it can take arguments.

static void Main(string[] args)
{
    //...
}
  
OR

static int Main(string[] args)
{
    //...
    return 0;
}
  
The parameter of the Main method, args, is a string array that contains the command-line arguments used to invoke the program. Unlike in C++, the array does not include the name of the executable (exe) file.

The call to ReadKey at the end of the Main method prevents the console window from closing before you have a chance to read the output.

C# programs generally use the input/output services provided by the run-time library of the .NET Framework. The statement System.Console.WriteLine("Hello World!"); uses the WriteLine method. This is one of the output methods of the Console class in the run-time library. It displays its string parameter on the standard output stream followed by a new line. Other Console methods are available for different input and output operations. If you include the using System; directive at the beginning of the program, you can directly use the System classes and methods without fully qualifying them. For example, you can call Console.WriteLine instead of System.Console.WriteLine

Command-Line Compilation and Execution

You can compile the "Hello World!" program by using the command line instead of the Visual Studio Integrated Development Environment (IDE). To compile and run from a command prompt
  1. Paste the code , and then save the file as a text file. Name the file Hello.cs. C# source code files use the extension .cs.
  2. Perform one of the following steps to open a command-prompt window: In Windows 10, on the Start menu, search for Developer Command Prompt, and then tap or choose Developer Command Prompt for VS 2017.

    A Developer Command Prompt window appears.
  3. In the command-prompt window, navigate to the folder that contains your Hello.cs file. Enter the following command to compile Hello.cs.
  4. csc Hello.cs
  5. If your program has no compilation errors, an executable file that is named Hello.exe is created. In the command-prompt window, enter the following command to run the program:
  6. Hello

Comments

Popular posts from this blog

System.Environment

System.Console

Datatype and keyword