An example of a simple application - Object-oriented programming

Example of a simple application

Consider an example of a simple console application that calculates the area of ​​a circle for a user-defined radius program:

using System;

namespace ConsoleApp

{

class Program {

static void Main () {

Console.WritefBBeAme radius of the circle: );

string s = Console.ReadLineQ;

double r = Convert.ToDouble (s);

double p = Math.PI * r * r;

Console.WriteLine ( Circle area = {0} & quot ;, p);

Console. ReadLineQ;

return;

}

}

}

If you save this code to a ConsoleApp.cs file (for example, using the standard Notepad application), you can use the C # - csc.exe compiler to create a managed application:

csc.exe ConsoleApp.cs

The output of ConsoleApp.exe, which was created by the compiler, is shown in Fig. 2.5.

The result of running a simple program

Fig. 2.5. The result of a simple program

The first line of the text of this simple program specifies which namespace will be used (in this case System). Namespaces are a way of grouping together related classes. For each program, you specify your namespace with the namespace keyword, which will be associated with all the classes described in the program. The using statement points to the namespace that the compiler should look for to find a description of the class that does not have a full name.

The reason for using the use statement in the above program is that it uses classes from the FCL library that are included in the System namespace. Using statement using System; allows you to reference the System.Console and System classes. Con vert, like Console and Convert, without specifying a namespace. The standard System namespace contains the basic .NET data types. It is important to understand that the functionality of the C # language is largely based on the base classes of the .NET platform. The C # language itself does not have built-in I/O as well as built-in types, but uses the base types described in the .NET FCL library.

The next line of the program in question declares a new ConsoleApp namespace, which describes one Program class:

namespace ConsoleApp {class Program {

}

}

The full name of this class is ConsoleApp.Program. All code in a C # program must be contained only within classes. The class description in C # consists of the keyword class, followed by the name of the class and a pair of curly brackets (block). All code associated with the class must be written between these braces. The Program class describes only one method named Main () (note that the first letter of the name is a large M):

static void Main () {...}

This method is automatically started (it is an entry point) when the program created by the compiler runs. It can return an integer value (int) or not return anything (void). This C # method corresponds to the main () method in C ++ and Java. The description of methods in C # has the following structure:

[modifiers] result_type_name method_name ([parameters])

{

// method content

}

Here, the square brackets indicate optional description elements. Modifiers are used to specify some features of methods, for example, such as where and as this method can be called. In this example, two modifiers are used: public and static. The modifier (access mode) public means that this method can be accessed from methods of any classes. The static modifier indicates that this method is not associated with a particular instance of the class and can be called without using an instance reference. It is important to run the program for execution without creating an instance of a particular class. In this example, the result type is void (meaning there is no result), and the Main () method does not describe the parameters passed.

Finally, consider the operators contained in the Main () method. The first statement prints prompts using the Write () method of the Console class:

Console.Write ( Enter the radius of the circle: ");

This method prints a line of text to the screen in the console window, but does not move the cursor to the beginning of the next line. To enter user data, the ReadLine () method of the same Console class is used to retrieve data from the keyboard (the variable s of string type is immediately declared):

string s = Console.ReadLine ();

The user can enter a number (to separate the fractional part, use a comma, not a period, as in the program code, for example: 2.5) and press the Enter key. The ReadLine () method returns a text string (string type). In order to convert the value of the text string s to a real value, for the variable r (which is also declared in the given string as a double type) the ToDouble (s) method of the Convert class is used:

double r = Convert.ToDouble (s);

The radius value obtained is used to calculate the area of ​​the circle that is stored in the variable p: double p = Math.PI * r * r;

Note that the calculation uses the value of the number n, which can be obtained from the static class System.Math of the FCL library.

To output the calculated value, use the WriteLine () method with a format string:

Console.WriteUnefnnoLMaAb circle = {0} & quot ;, p);

Finally, in order for the console window to not close automatically after executing the program, the ReadLine () method is used, which pauses the program execution before the Enter key is pressed.

The return statement is then executed, which causes the method to terminate. Usually this operator returns the result value, but since this method has a type of void in the header, no value is returned. In this case, this operator could not be written down.

thematic pictures

Also We Can Offer!

Other services that we offer

If you don’t see the necessary subject, paper type, or topic in our list of available services and examples, don’t worry! We have a number of other academic disciplines to suit the needs of anyone who visits this website looking for help.

How to ...

We made your life easier with putting together a big number of articles and guidelines on how to plan and write different types of assignments (Essay, Research Paper, Dissertation etc)