Welcome to this C # tutorial, with the introduction of the .NET framework, Microsoft had a new language called C # (cc acute term). C # is designed for a simple, modern, general-purpose, object-oriented programming language, which is borrowing key concepts from many other languages, most notably Java.
C # can theoretically be compiled into machine code, but in real life, it is always NET structure is used in combination. Therefore, applications written in C # need a .NET framework to be installed on the computer running the application. While the .NET framework makes it possible to use a wide range of languages, C # is sometimes referred to as the .NET language, possibly because it was created with the framework
C # is an object-oriented language and does not offer global variables or functions. Everything is wrapped in the classroom, even ordinary types like int and string, which are received from the system. Object Class.
C # can be written with any text editor, such as Windows Notepad, and then the C # command line compiler, csc.exe, which comes with the .NET framework. However, most people like to use an IDE (integrated development environment), and Microsoft offers several options for it. Their flagship is Visual Studio, which can be used to work on every possible aspect of the NAT framework. This product is very advanced, and comes in many versions. Visual Studio is not really cheap, and hobby can be advanced for programmers too.
With .NET Framework 2.0, Microsoft launched the so-called Expressions versions, which were targeted by hobby programmers and those who wanted to try NIT. And they continued this tradition with the release of NIT 3.0 and 3.5. The Express Edition works only for one language, such as C # or VB.NET, and recall some of the advanced features of Visual Studio. However, they are free and will work fine to learn languages, which is why we will use it for this tutorial.
For C# programming, you should download the Visual C# Express from http://www.microsoft.com/express/download/. Install it, and you're ready to write your first C# application!
Example
If you have ever learned the programming language, then you know that they are all "Hello, world!" Let's start with, for example, and who are we to break such a good tradition? Start Visual C # Express (presented in the previous chapter), and choose File -> New Project ... From the project dialog, select Console Application. This is the most basic application type on the Windows system, but do not worry, we will not be here for a long time. Once you click OK, Visual C # Express creates a new project for you, which includes a file called program.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
}
}
Actually, all of these rows do not really achieve anything, or at least it may seem like trying to run the app by pressing F5 on your keyboard, this view will compile and execute C # Express on your keyboard, But as you will see, it does not do much, you will probably see a black window launch and close again. This is because our applications do not do anything yet in the next chapter we will see through these lines what they all are about, but for now, if we really want to see some results, then show us that we All know about # and add two rows to get some output. . In the last set of {}, add these lines:Console.WriteLine("Hello, world!");
Console.ReadLine();
The code of your first application should now look like this:using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
Console.ReadLine();
}
}
}
Once again, hit F5 to run it, and you'll actually see the black window that lives, and even our greeting for the world is showing well, so we added two lines of code, but they What to do? One of the good things about the C # and NAT framework is the fact that many codes are also understood for untrained eyes, which is illustrated by this example.
The first line uses the console square to produce a line of text, and the second reads a line of text from the console. Read? Why? Actually, this is a bit of a trick, because without it, this application only ends and closes the window with the output before someone can see it.
The readline command asks the application to wait for the input from the user, and as you will see, the console window now allows you to enter text. Press Enter to close it Congratulations, you have just created your first C # application! For more information about what is actually happening, read the next chapter.
Example explained
In the last chapter, we tried to write a piece of text to the console, to see some real progress in our first C # application, we did not take too much details about the lines of code, so this chapter is the world example code There is an explanation. As you might see from the code, some lines look the same, so we will bring them back to the group for a personal explanation. Let's start with the least and most common characters in our code: {and} they are often referred to as curly braces, and in C #, they mark the beginning and end of a logical block of code. Curl braces are used in many other languages, including C ++, Java, Javascript and many other languages. As you can see in the code, they are used to wrap many lines that meet together. In later examples, it will be clear how they are used.Now start from the beginning:
using System;
using System.Collections.Generic;
using System.Text;
Is a keyword used by the editor, highlighted with blue color. The keyword uses a namespace, and is a collection of namespace classes. Classes bring us some kind of functionality, and when we work with an advanced IDE like Visual C # Express, it will generally create parts of the trivial code for us. In this case, we created a class for us , And imported names that are required or are expected to be used in general. In this case, 3 namespaces are imported for us, each of which is imported There are very useful classes in For example, we use console classes, which are part of the system namespace.As you can see, we also get our namespace:
namespace ConsoleApplication1
Namespace Console Application 1 is now the main namespace for this application, and new classes will be a part of it by default. Obviously, you can change it, and you can create a square in the other name space. In that case, you have to do this like the other namespace, with the statement to use this new namespace in your application.Next, we define our class since C # is actually an object-oriented language, which, in fact, every line of code actually does something, it is wrapped inside a class. In this case, the class is simply called the program:
class Program
We can do even more classes, even in the same file. For now, we only need one class. In a class, many variables, properties and methods, concepts can be included which we will later in depth. For now, you need to know that there is only one way in our current class and nothing else has been declared as such:static void Main(string[] args)
This line is probably the most complex in this example, let's divide it a little bit. The first word is static, the static keyword tells us that this method should be attainable without a class, but about this in our chapter about classes moreThe next keyword is zero, and tells us what the method should return. For example, int can be an integer or a string of text, but in this case, we do not want our method to return anything, or zero, which is not equal to any type.
The next word is main, which is simply the name of our method. This method is the so-called entry point of our application, i.e., the first part of the code to be executed, and in our example, the only piece to execute
Now, after the name of a method, the set of arguments can be specified within the set of brackets. In our example, our method only takes one argument, which is called AGR. The type of argument is a string, or more accurate, an array of strings, but more on that later. If you think about it, it makes sense, because Windows applications can always be said with an optical set of arguments. These arguments will be passed as the text string of our main method.
0 comments:
Post a Comment