📜 ⬆️ ⬇️

We learn C #. Third bucket

Feedback results


Pro audience


Since there were a lot of comments about this, I decided to make a last attempt. I hope for feedback in the comments - it turned out “For All” or not.

About books


After reading the comments I decided to publish a list of books that were recommended there:
C # 2008. Crash course
CLR via C #
C # and the .NET platform
C # 2.0. Complete guide

About different


There were questions about Mono speed. recently found a link to which there is a small test. geekswithblogs.net
Testing was conducted on earlier versions, but I think the situation has not changed globally. IMHO
')
Well, now let's continue learning ...

Complex types


C # allows you to define your own complex types based on the available simple types. Like primitives, complex types can be divided into types by reference and by value.

Types by value


Structures


In C #, a structure is a special type of class. Since the structure is placed on the stack, it can be created and copied more efficiently than a class. Consider a small structure:
  1. public struct Subscriber { public long SubID; public string FirstName; public string LastName; public string MiddleName; public decimal Balance; }
  2. public struct Subscriber { public long SubID; public string FirstName; public string LastName; public string MiddleName; public decimal Balance; }
  3. public struct Subscriber { public long SubID; public string FirstName; public string LastName; public string MiddleName; public decimal Balance; }
  4. public struct Subscriber { public long SubID; public string FirstName; public string LastName; public string MiddleName; public decimal Balance; }
  5. public struct Subscriber { public long SubID; public string FirstName; public string LastName; public string MiddleName; public decimal Balance; }
  6. public struct Subscriber { public long SubID; public string FirstName; public string LastName; public string MiddleName; public decimal Balance; }
  7. public struct Subscriber { public long SubID; public string FirstName; public string LastName; public string MiddleName; public decimal Balance; }
  8. public struct Subscriber { public long SubID; public string FirstName; public string LastName; public string MiddleName; public decimal Balance; }

If we need to copy from one structure to another, then simply put an equal sign. Let's create one Subscriber object, Sub1, and set the field values. After that, copy the value of all fields from one structure to another, Sub2:
  1. Subscriber Sub1;
  2. Subscriber Sub2;
  3. // Initialize the structure and fill in the fields
  4. Sub1 = new Subscriber ();
  5. Sub1.FirstName = "Vasya" ;
  6. Sub1.LastName = "Pupkin" ;
  7. Sub1.MiddleName = "Some" ;
  8. Sub1.Balance = 100;
  9. // And now one line copy the contents of one structure to another
  10. Sub2 = Sub1;

Notice that we used new , although the structure is type by value? Just access to the structure is possible before calling the operand new .
In addition to copying, the structure makes function calls more accurate.

If you are a C or C ++ guru, be careful - they have changed! In C / C ++, the only difference between class and structure was that they were public, not closed.
A string in C # is a completely new toy, not only because its default members are closed, but instances of structures and instances of classes are placed in different places of memory.
Structures can support most of the class functions (although it does not support the inheritance of implementations), but they must be used carefully. Structures are best suited for providing small objects.

Transfers


Enumerations are the integer types that the user defines. When listing declarations, you specify a set of valid values ​​that can accept instances of enumerations. In addition, you need to assign intuitive names to the values.
In future work, enumerations can play a very important role and make life easier for the programmer. There are listings like this:
  1. public enum DaysOfWeek
  2. {
  3. Monday = 0,
  4. Tuesday = 1,
  5. Wednesday = 2,
  6. Thursday = 3,
  7. Friday = 4,
  8. Saturday = 5,
  9. Sunday = 6
  10. }

Here we use integer values ​​that correspond to the days of the week. You can get access to a specific day of the week like this: DaysOfWeek.Wednesday returns 2. Usually, enumerations are used when you need to transfer the corresponding value to a method that will pass through all values ​​using a switch and produce the corresponding result. We'll talk about switch in more detail a little later, but for now let's see an example:
  1. class program
  2. {
  3. static void Main ( string [] args)
  4. {
  5. WriteText (DaysOfWeek.Sunday);
  6. Console .ReadLine ();
  7. }
  8. static void WriteText (DaysOfWeek days)
  9. {
  10. switch (days)
  11. {
  12. case DaysOfWeek.Monday:
  13. Console .WriteLine ( "Monday is a hard day!" );
  14. break ;
  15. case DaysOfWeek.Tuesday:
  16. Console .WriteLine ( "Tuesday - this means that Monday has already passed!" );
  17. break ;
  18. case DaysOfWeek.Wednesday:
  19. Console .WriteLine ( "Wednesday! Mid week!" );
  20. break ;
  21. //And so on...
  22. }
  23. }
  24. }


Types by reference


Classes


Classes are the primary user-defined type in C # and the .NET platform. Almost all programs have at least one class (theoretically, it is possible to use a structure instead of a class), which contains the Main () method - the entry point to the program.
Classes are composite data types that include data members and functions. classes also contain nested data types. In more detail about the classes let's talk through one "bucket". =)

Interfaces


The interface in C # contains only abstract elements that have no implementation. Directly the implementation of these elements should be contained in a class derived from this interface.
C # interfaces can contain methods, properties, and indexers, but unlike, for example, Java, they cannot contain constant values.
For example, if an interface contains a method, it will work, but the code of the method implementation inside it will not be.

Delegates


Delegates are types that refer to methods. They are similar to function pointers in C ++, but allow you to create an instance of a class and call both static methods and methods of a specific instance of a class.
For beginners, it is still too early to delve into the subtleties, so just remember the name - we'll return to them.

Arrays


An array specifies how to organize the data. An array is an ordered collection of elements of the same type. Each element of the array has indices that determine the order of the elements. The number of indices characterizes the dimension of the array.
In C #, as in many other languages, indices are specified by an integer type.
In C #, a significant limitation of the C ++ language to the static character of arrays has been removed. C # arrays are true dynamic arrays.

Array declaration

Consider how one-dimensional arrays are declared.

Declaring one-dimensional arrays

The overall structure of the ad:
[<attributes>] [<modifiers>] <type> <name>;

Let's forget about attributes and modifiers. The one-dimensional array declaration is as follows:
<type> [] <name>;

Note that, unlike C ++ language, square brackets are not assigned to a variable name, but to a type. They are an integral part of the class definition, so writing T [] should be understood as a one-dimensional array class with elements of type T.
  1. // array declaration
  2. int [] Integers;

This is an example array declaration. You can also initialize an array using new :
  1. // Create an array of 32 int values
  2. int [] Integers = new int [32];

To gain access to an array element, the usual syntax is used:
  1. Integers [0] = 24;

C # allows us to create arrays without initializing them, so they can change the size during program execution.
  1. int [] Integers;
  2. Integers = new int [32];

Arrays in C # retain the ability, as in C / C ++, to initialize the values ​​listed in curly braces.
  1. string [] Privet = { "one" , "two" , "three" };
  2. // Which is equivalent
  3. string [] Privet = new string [] { "one" , "two" , "three" };

However, there are pitfalls! For example, to set the size of an array you cannot use a variable:
  1. int len = 3;
  2. // will not compile
  3. string [] Privet = new string [len] { "one" , "two" , "three" };
  4. / * However, we can use a constant * /
  5. const int len = 3; // This is a constant
  6. // Now compiles
  7. string [] Privet = new string [len] { "one" , "two" , "three" };


What interesting things can you do with an array?
Find out the length of the array:
  1. int Dlina = Massiv.Length;
  2. // ToString () - translates from int to string
  3. Console .WriteLine (Dlina.ToString ());

Sort:
  1. // Our array is passed as a parameter to the Sort () method
  2. Array.Sort (Massiv);

Change the order:
  1. // Our array is passed as a parameter to the Reverse () method
  2. Array.Reverse (Massiv);

and much more ...

I recommend reading multi-dimensional arrays in the book yourself.

So today I think enough. Do not believe it, but missed one day due to the fact that after typing this (similar) article in Habra Editor I had something to turn off the browser and I did not have time to save. Very angry and went to sleep. Emotions ...

Source: https://habr.com/ru/post/49808/


All Articles