Learning C #. The second bucket.
Feedback Results
A bit about C # and * nix
Many were interested in how to write in C # on * nix systems. I will try to help our friends. There is a Mono project and here's what Wikipedia tells us:
Mono is a project to create a full-fledged implementation of the .NET system based on free software.
Mono includes the C # compiler, mcs, the .NET runtime mono (with JIT support) and mint (without JIT support), a debugger, and a number of libraries, including the implementation of ADO.NET and ASP.NET. The project also develops bindings for the GTK + graphics system on the .NET platform.
The mono runtime can execute modules written in C #, Visual Basic .NET, Java, Boo, Nemerle, Python, JavaScript, PHP, and Object Pascal (if you have a compiler in the .Net / Mono environment). Support for C, Ada 2005, and Eiffel is also expected.
Mono implementations exist for the following operating systems: GNU / Linux, Solaris, Mac OS X, Microsoft Windows, and Unix.
And this tells us that we can easily program in almost any C # OS. The latest release of Mono Project is 2.2. Mono Project - here you can download the latest version, as well as see the documentation.
There is also more good news. The Mono development team has a subsidiary project, MonoDevelop . This is a free IDE for C # and ASP.NET. It supports all the necessary functions for successful and rapid development in * nix systems.
My Neighbor - My Friend
Since an article on programming languages such as Ruby and Python is being written in the neighborhood, we should be friends with our neighbors.
IronRuby
IronRuby is an implementation of the Ruby programming language on the Microsoft .NET platform. The interpreter is based on Dynamic Language Runtime, a library running on CLR 2.0 that provides, among other things, dynamic typing and dynamic method definition.
IronPython
IronPython —— one of the main implementations of the Python language, designed for the Microsoft .NET or Mono platform. It is completely written in C #, and is a compiler type compiler.
IronPython can use .NET types. You can also use IronPython code from .NET code, either by hosting the IronPython system, or by pre-assembling IronPython code.
IronPython
A read?
From books, C # for professionals has become my native . Authors: Simon Robinson, Oldie Cornes, Jay Glyn, Barton Harvey et al. I started right from her, but just as I had programming experience and if you don't have one, I recommend reading C # Author: Carly Watson.
C # Posted by Carly Watson.
C # for professionals. Authors: Simon Robinson et al.
Now let's continue ...
C # Data Types
Wikipedia
A data type (the term data type is also found) is a fundamental concept in programming theory. A data type defines 1) a set of values, 2) a set of operations that can be applied to such values, and, possibly, 3) a way to implement the storage of values and perform operations. Any data that programs operate on is of a certain type.
C # is a strongly typed language. When using it, you must declare the type of each object that you create (for example, integers, floating-point numbers, strings, windows, buttons, etc.), and the compiler will help you to avoid errors associated with assigning only values to variables the type that matches them. The type of the object tells the compiler the size of the object (for example, an object of type int occupies 4 bytes in memory) and its properties (for example, a form can be visible and invisible, etc.).
To better understand what they are for, I will give an example. Type is the same variety. For example, a variety of apples. An apple of one sort can be large, contain n iron and be red, and of another variety be small and be violet =)
C # also divides types into two other categories: dimensionaland reference .
Built-in types
The C # language provides the programmer with a wide range of built-in types. They all comply with the CLS (Common Language Specification) and this ensures that objects created in C # can be successfully used along with objects created in any other programming language that supports .NET. Each type has a strictly defined size for it, which cannot be changed.
Let's look at the table of built-in types: In the first column is the name of the type, then the range of values (from how many to how many) and finally the size.

Converting Inline Types
Objects of one type can be converted to objects of another type implicitly or explicitly. Implicit conversions happen automatically; the compiler does this instead of you. Explicit conversions occur when you “cast” a value to a different type. Implicit conversions also ensure that data is not lost. For example, you can implicitly cast from short (2 bytes) to int (4 bytes).
Again, a small example. Let us have a bucket and a cup. =) If we pour water from a cup into a bucket, then we get an “implicit transformation”, and if from a bucket into a cup, then only “explicitly”. But how is it possible? We just have all the rest of the water pouring out.
No matter what the value is in short, it will not be lost when converting to int:
- short x = 1;
- int у = х; //неявное преобразование
If you do the inverse transformation, then, of course, you can lose information. If the value in int is greater than 32.767, it will be truncated during conversion. The compiler will not perform an implicit conversion from int to short:
- short х;
- int у = 5;
- х = у; //не скомпилируете
You must perform the explicit conversion using the cast operator:
- short х;
- int у - 5;
- х = (short)у; //А вот теперь всё ОК
Types by reference
Type Object
In C #, the type object is the original ancestor type for all types. The object link can be used to bind to any object. It is also useful in reflection when the code should work with objects whose type is not known.
Object provides us with some great methods. Equals () , GetHashCode () , GetType () and ToString () . These methods are available to any object and we will talk about them later.
Type String
C # has its own type string. Therefore, operations such as copying and merging occur instantly.
- string s1 = "Здравствуй, ";
- string s2 = "Хабра";
- string s3 = "хабр!";
- string s4 = s2 + s1 + s3;
- Console.WriteLine(s4); // => Здравствуй, Хабрахабр!
Although the assignment takes place in the style of types by value, the type System.String is a type by reference. We’ll come back to the lines!
Variables
A variable is a location in memory of an object of a particular type. In the above examples, x and y are variables. Variables can have values with which they are initialized, or these values can be changed programmatically.
To create a variable, you must specify the type of the variable and then give that type a name. You can initialize a variable during its declaration or assign a new value to it during program execution. Here is an example program that, in the first case, uses initialization to assign a value to a variable, in the second case, uses the assignment of a value to a variable using the "=" operator:
- class Variables
- {
- static void Main()
- {
- int myInt = 10;
- System.Console.WriteLine("Инициализированная переменная myInt: " + myInt);
- myInt = 5;
- System.Console.WriteLine("Переменная myInt после присвоения значения: " + myInt);
- Console.ReadLine();
- }
-
- }
The result of this program will be as follows: The
initialized variable mylnt: 10
myInt after assigning the value: 5
Here is the line:
int myInt = 10;
means declaring and initializing the mylnt variable. String:
myInt = 5;
means assigning the variable mylnt the value 5.
C # requires the definition of values, that is, the variables must be initialized before use. To test this rule, consider the following example:
- class Variables
- {
- static void Main()
- {
- int myInt;
- System.Console.WriteLine("Неинициализированная переменная myInt: " + myInt);
- myInt = 5;
- System.Console.WriteLine("После присвоения переменной myInt: " + myInt);
-
- }
- }
If you try to compile this example, the compiler will display the following error message:
error CS3165: Use of unassigned local variable 'mylnt'
You cannot use an uninitialized variable in C #!
Or maybe try?
So let's write a small program. What will she do? Display a short dossier per person:
- using System;
-
- class First
- {
- string country, city;
- string name = "Вася Пупкин";
- int old, num;
-
- old = 12;
- num = 666;
- country = "Russia";
- city = "Наша";
-
- Console.WriteLine("Имя: " + name);
- Console.WriteLine("Возраст: " + old);
- Console.WriteLine("Локация проживания: " + country + city);
- Console.WriteLine("/n/tСерийный номер: " + num);
- Console.ReadLine();
- }
First comes the declaration of variables and initialization. Following is the conclusion of these changes. On line 17, you come across two new characters for you. These are / n and / t. The first is used to translate the bracket to a new line, and the second is a tab. Below is a character table that you can use. On line 18, we added Console.ReadLine () - this is done so that after starting the program the console does not close. That's all for today. PS Just found for someone perhaps an interesting link. C # for geeks. (MSDN)
