N suggestions for improving C # Program (6-10)

Source: Internet
Author: User

Recommendation 6: differentiate the usage of readonly and const.

First, differentiate their respective features.

Readonly: Number of runtime periods.

Const: a constant during the compilation period (which determines that it must be static). It can only modify the primitive type (string also belongs to the primitive type) and enumeration type.

Some may ask, what is the difference between the runtime constant and the compilation constant? Let's take a look at the code first.

// Code snippet public static readonly int readonlyVar = 100; public const int constVar = 99; Console. writeLine (readonlyVar. toString (); Console. writeLine (constVar. toString ());

After compilation:

public const int constVar=0x63;public readonly int readonlyVar;Console.WrintLine(this.readonlyVar.ToString());Console.WrintLine(this.0x63.ToString());

After compilation, we can see that the place where constVar is called is replaced by "0x63", while readonlyVar is also the variable name.

The Running frequency is measured only when the program runs, and the running frequency is referenced by the constant when the IL is generated.

The value of a constant during the compilation period.

Const Environment: A value in the system remains unchanged. for example.. NET Framework Math. PI (1, 3.14159 ). (Note: If you modify its value, re-compile all the projects that reference it ).

Readonly environment: a constant that remains unchanged in the life cycle of an object that requires a value assignment.

 

Recommendation 7: use 0 as the default enumeration value.

public enum Week{     None=0,     Monday=1,     Tuesday=2,     Wednesday=3,     Thursday=4,     Friday=5,     Saturday=6,     Sunday=7 }
We recommend that you set the default value to "0" When Week is not assigned a value. This processing method is better, but it is only a good improvement.

Recommendation 8: Avoid providing explicit values for enumeration elements.

I personally do not agree with this suggestion. I think it is clear to assign values explicitly for the code.

 

Recommendation 9: you must know how to overload operators.

For example, how do I add two Salary instances?

public class Salary        {            public int RMB { get; set; }            public static Salary operator +(Salary s1, Salary s2)            {                s2.RMB += s1.RMB;                return s2;            }        }

In this way, you can call:

Salary s1 = new Salary() { RMB = 100 };Salary s2 = new Salary() { RMB = 200 };Salary s3 = s1 + s2;

This is the basic knowledge of C # development. Maybe it is not commonly used, but it must be known.

 

Recommendation 10: you must know how to implement a class comparator.

The so-called comparator is just an interface, and only one method (public int CompareTo (object obj) needs to be implemented. What is the usage of the comparator? In addition to comparing the sizes of two instances as the name suggests, it can also be used for sorting (for example, the List set, you can directly call the. Sort () method. This is very convenient. Define Salay class:

public class Salary : IComparable<Salary>        {            public int RMB { get; set; }            public int CompareTo(Salary obj)            {                if (this.RMB > obj.RMB)                {                    return 1;                }                else if (this.RMB == obj.RMB)                {                    return 0;                }                else                {                    return -1;                }            }        }

Call:

List<Salary> salarys = new List<Salary>();            for (int i = 0; i < 5; i++)            {                Salary salary = new Salary() {RMB=100*i };                salarys.Add(salary);            }            salarys.Sort();            foreach (var item in salarys)            {                Console.WriteLine(item.RMB);            }

Sorting becomes so simple.

Related Article

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.