Learning C #. Bucket One.

    Hello everybody


    I decided to start a series of articles on learning programming in .NET, namely in C #. I do not claim authorship of everything that will be written hereinafter, I just want to give knowledge to those who want it.
    On Habré many times they could notice my comments about the fact that I would really like to educate people, but I couldn’t think of how to do it. After a series of MaxElc articles , I thought that I, too, could start writing in parallel with it.
    Let's start!

    .NET architecture


    .NET is a runtime or platform. It is located between the code and Windows, allowing you to provide the necessary services. The .NET platform consists of two main components. These are the Common Language Runtime and the .NET Framework Class Library .

    Common Language Runtime (abbreviated CLR) can be called the "engine" of the .NET platform. CLR deals with memory management, compilation and execution of code, work with control flows, security, etc.

    The .NET Framework Class Library is a collection of classes for all occasions.


    So, to understand how .NET works, consider its components:
    General type system(Common Type System, abbreviated CTS) - covers most of the types found in common programming languages.
    The virtual execution system (Virtual Execution System, abbreviated VES) - is responsible for loading and executing written for CLI programs.
    System metadata (Metadata System) - is intended to describe the types of stored independent of the particular programming language.
    Common intermediate language (Common Intermediate Language, abbreviated CIL) - a platform-independent object-oriented bytecode, acting in the role of the target language to any supported CLI compiler.
    General language specification(Common Language Specification, abbreviated CLS) is an agreement between developers of programming languages ​​and developers of class libraries, which defines a subset of CTS and a set of rules.

    One of the main ideas of .NET is the compatibility of various services written in different languages. For example, a service written in C ++ for .NET can access a class method from a library written in Delphi; in C # you can write a class that inherits from a class written in Visual Basic .NET, and an exception thrown by a method written in C # can be caught and processed in Delphi.

    I will not go into details and reprint textbooks. For those who wish to learn more about .NET, I will leave links to the necessary resources below. It's just that the whole point is that .NET helps us! And this is very good. =)

    C # Basics



    For convenient execution of examples, I recommend that you download the free Visual Studio 2008 Express , and for those who do not have official traffic, you can use #Develop - this is an OpenSource project with open source code.

    Let's start with a slightly scary example already - Hello World!
    1. using System;
    2.  
    3. class HelloWorld
    4. {
    5.   public static int Main()
    6.   {
    7.     Console.WriteLine("Hello World!");
    8.     return 0;
    9.   }
    10. }
    * This source code was highlighted with Source Code Highlighter.

    If you are using Visual Studio or #Develop, create a new console project. For perverts :) you can try using csc.exe - this is the C # compiler. It is part of the freeware MS.NET SDK.
    To build in the IDE, select Build from the Project menu, and using csc.exe type in the command line:
    csc /out:TargetFile.exe SourceFile.cs

    where TargetFile.exe is the output file, and SourceFile.cs is the file where you typed the above code.

    Let's take a look at the code we wrote.
    using connects the System namespace , which includes the I / O methods and the required types.
    class HelloWorld - announced a new class, which we called HelloWorld. Later I will tell you more about classes in more detail, but for now you need to understand that any C # program consists of classes.
    Next comes the Main () method - this is the door for the compiler. He will start his work from here. The Main () method is required for any program and its name should be written that way. In C #, case matters;).

    [modifier] return_type MethodName ([parameter])
    {
    // method body
    }


    Here's what the methods look like. First comes the modifier, then you need to specify the type of data that it will return, the name of the method itself, and in brackets the parameters that it takes. You will learn about all this from future articles, but for now, just try to remember.
    Square brackets indicate optional parameters. Modifiers are used to indicate the particularities of a method call, for example, where it can be called from.

    Finally, let's move on to the operators themselves. We call the WriteLine () method of the System.Console class to display a string in the console. The return statement is used to exit the method, and since this is the Main () method, the program exits. The header of the Main () method indicates the type int, so you need to return an integer value (in this case).

    Important links


    .NET Framework (Wikipedia)
    Off. .NET Website
    Overview of the .NET Framework Technology (MSDN)
    Visual Studio 2008 Express
    #Develop
    C # IDE Mobile

    P.S. I left this article logically unfinished because tomorrow I intend to continue to write and publish. Write your criticism and questions in the comments. I hope you enjoyed it.
    PPS MaxElc ! And the buckets are more interesting =)

    Also popular now: