Here is the code. What does, I can not understand. Yes, the methods do serialization and back, but, for example, what the string List<ListNode> arr = new List<ListNode>(); does does List<ListNode> arr = new List<ListNode>(); ?

 public void Serialize(FileStream s) { List<ListNode> arr = new List<ListNode>(); ListNode temp = new ListNode(); temp = Head; //transform nodes into List do { arr.Add(temp); temp = temp.Next; } while (temp != null); //write into file; data is modify for store index of .Random node using (StreamWriter w = new StreamWriter(s)) foreach (ListNode n in arr) w.WriteLine(n.Data.ToString() + ":" + arr.IndexOf(n.Rand).ToString()); } public void Deserialize(FileStream s) { List<ListNode> arr = new List<ListNode>(); ListNode temp = new ListNode(); Count = 0; Head = temp; string line; //try read file and create List of nodes try { using (StreamReader sr = new StreamReader(s)) { while ((line = sr.ReadLine()) != null) { if (!line.Equals("")) { Count++; temp.Data = line; ListNode next = new ListNode(); temp.Next = next; arr.Add(temp); next.Prev = temp; temp = next; } } } //declare Tail Tail = temp.Prev; Tail.Next = null; //return refs to Random nodes and restore Data foreach (ListNode n in arr) { n.Rand = arr[Convert.ToInt32(n.Data.Split(':')[1])]; n.Data = n.Data.Split(':')[0]; } } 
  • What do you think in the specified line quotes, angle brackets? - Kromster

1 answer 1

In these quotes, a generic type is written. In the tooltips, you will only see a list of this type. List<T> here T is the same generalized type that allows you to work with your favorite data type; you only need to specify it in advance in the class declaration. What is this for? As an example, you will not see a generic method not a class on a method, but the essence of this is almost unchanged:

 void Swap<T>(ref T a,ref T b){ T temp = a; a = b; b = temp; } 

The well-known method of changing variables with values. But what is there doing some type T in triangular quotes? This is something like object but during a method call, the user must specify in these quotes what type of data the method takes as an argument. Yes, of course, could you overload the method for all the types that you will use? But why (if you are not Hindu over), if you can make such an elegant and simple code. The System.Collections.Generics class library contains several classes for working with generic types, such as: List<T> , Dictionary<K,V> , Queue<T> , Stack<T> and so on. You probably managed to notice some other letters besides Π’ in Dictionary<K,V> . Yes, this is almost no difference and no. You may wish to call this type. But it is accepted that if type 1 is called T , and if 2 then T,U


Now about the code. If specifically about the code you asked to explain in the comments:

 List<ListNode> arr = new List<ListNode>(); //Бписок экзСмпляров класса ListNode (динамичСский массив) ListNode temp = new ListNode(); //Новый экзСмпляр класса ListNode temp = Head;//ΠŸΡ€ΠΈΡΠ²Π°ΠΈΠ²Π°Π½ΠΈΠ΅ экзСмпляру класса ListNode Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Head //Π’ Ρ†ΠΈΠΊΠ»Π΅ ΠΌΡ‹ всС ΡƒΠ·Π»Ρ‹ ΠΈΠ· Ρ„Π°ΠΉΠ»Π° пСрСносим Π² список для дальнСйшСй Ρ€Π°Π±ΠΎΡ‚Ρ‹ с Π½ΠΈΠΌ. do { arr.Add(temp);//Π’ΡƒΡ‚ ΠΌΡ‹ добавляСм Π² список ΡƒΠ·Π΅Π» temp = temp.Next;//Π’ΡƒΡ‚ ΠΌΡ‹ ΠΏΠ΅Ρ€Π΅Ρ…ΠΎΠ΄ΠΈΠΌ ΠΊ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰Π΅ΠΌΡƒ } while (temp != null);//Пока ΡƒΠ·Π»Ρ‹ ΡΡƒΡ‰Π΅ΡΡ‚Π²ΡƒΡŽΡ‚ 

I think my little story helped you understand the purpose of generic types. You can read more about them at this link.

SeeSharp

  • Thank you, they helped a lot, but you can help with the code, I superficially understand it, this is serialization and deserialization, we read and write, and nothing else is clear) - Andrew
  • @ Andrei updated the answer - Aqua