C # small "traps"

Source: Internet
Author: User

C # small "traps"
1. obtain the current running path of the program. Recurrence: the WPF client program cannot enter the main interface after it is started. It is stuck in the initial Screen (Splash Screen) to solve the problem: when loading an icon through logs, A Bug is skipped. The initial code is as follows: var icon = new Icon ("Images \ xxx. ico"); very simple. It seems that there is no problem, and the relative directory is correct. Double-click the program to start the program normally, and Debug to start the program completely normally. Otherwise, this Bug has long been detected. The error in the log during Automatic startup is: the file "C: \ Windows \ System32 \ Images \ xxx. ico" is not found ??? This is confusing. How does the relative directory in the program go to sysem32? If the directory is incorrect and the file cannot be found, the Exception is entered. The first reaction is that the relative Directory may not be reliable, and the following code is changed: var icon = new Icon (Directory. getCurrentDirectory () + "\ Images \ xxx. ico "); // var icon = new Icon (Environment. currentDirectory + "\ Images \ xxx. ico "); Haha, it still does not work. In another way (the second sentence to be commented), the error is the same. Both methods return the path "C: \ Windows \ System32. In fact, Environment. CurrentDirectory calls the Directory. GetCurrentDirectory () method internally. Solution: StackOverflow has a discussion on this issue. Application in WinForm. startupPath also has the same problem. The following is a recommended method for obtaining the current directory: var path = Path. getDirectoryName (Assembly. getExecutingAssembly (). location); 2. IEnumerable should correctly use the software design. There is a very important principle: High Cohesion and low coupling. Therefore, we often face Interface Programming and open the underlying interface as much as possible. Therefore, IEnumerable is often used, because the return values of many methods in the Linq operation are IEnumerable <T>. This trap is about IEnumberable. Check the following code to see if the returned value is the same as what you want: Copy code 1 internal class Program 2 {3 private static void Main (string [] args) 4 {5 //... 6 var students = GetStudents (); 7 foreach (var student in students) 8 {9 student. isActived = true; 10 Console. writeLine (student. isActived); 11} 12 13 foreach (var student in students) 14 {15 Console. writeLine (student. isActived); 16} 17 18} 19 20 private static IEnumerab Le <string> GetNames () 21 {22 //.... 23 return new [] {"AAA", "BBB", "CCC"}; 24} 25 26 private static IEnumerable <Student> GetStudents () 27 {28 //... 29 return GetNames (). select (s => new Student (s); 30} 31 32 public class Student33 {34 public string Name {get; set;} 35 public bool IsActived {get; set ;} 36 37 public Student (string name) 38 {39 Name = name; 40} 41} 42 43} copy the code. The first foreach will output three true values, which is undoubtedly true. In the second foreach, is there any output of three true? If so, this is not a trap:-(continue to look at the following code: copy code 1 var studentList = GetStudents (). toList (); 2 studentList [0]. isActived = true; 3 var studentsActived = studentList. where (s => s. isActived); 4 5 Console. writeLine (studentsActived. count (); 6 7 studentList [0]. isActived = false; 8 9 Console. writeLine (studentsActived. count (); duplicate code outputs 1? Is there a feeling that the world is revolutionizing ...... I will not explain it here. Let's talk with the Code. Continue to read the code and modify the GetStudents () method: copy the Code 1 private static IEnumerable <Student> GetStudents () 2 {3 //... 4 return GetNames (). select (s => 5 {6 var stu = new Student (s); 7 Console. writeLine (s + ":" + stu. getHashCode (); 8 return stu; 9}); 10} copy the Code. In the code above, the GetStudent () method is called twice, how many times do you think HashCode will input 6 times? The output is 9 times. The three times in Area 3 are due to the call of GetStudents (). the ToList () method, Region 1 and Region 2 are output by the previous two foreach runtime, and each HashCode is different, indicating that each is a different instance. Think about whether there is a Lazy Loading in Entity Framewor. Every time an object in the set is used, an SQL statement is executed to find the object from the database. Here is the truth: IEnmerable can be understood to only store the computing expression of the set. When using the object in the Set, it will find the object based on the calculation. Because the GetStudents () function uses the Select method, It will be updated every time it is used. This is why each HashCode above is different. In Linq, The ToList () Extension Method is equivalent to executing the computation expression in IEnumerable and loading all objects to the set. This is the real set.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.