[Tips] to make null value processing of C # more elegant,

Source: Internet
Author: User

[Tips] to make null value processing of C # more elegant,


Http://www.codeproject.com/Articles/739772/Dynamically-Check-Nested-Values-for-IsNull-Values? Msg = 4895299 # xx4895299xx




Processing of null values in C # has always been an unpleasant task. Suppose there are the following classes:

public class Union { public string Name { get; set; } }  public class Dep {        public Union Union { get; set; }        public string Name { get; set; } }  public  class Person    {        public  Dep Dep { get;set;}        public string Name { get; set; }    }


If Person. Dep. Union. Name. Length> 5, write the log. How can I write the code? This is probably the case:

 if (person != null && person.Dep != null && person.Dep.Union != null && person.Dep.Union.Name != null && person.Dep.Union.Name.Length > 5)            {                Console.WriteLine(person.Dep.Union.Name);            }


If you write many logical statements, you should have a deep understanding of the complexity of such writing.



1. Before accessing an object, the extension method first enters the corresponding static method. This parameter can be easily judged and processed without causing nullreference exceptions;

2. The delegation can perform subsequent actions of the extension method well.

We can write the following statement in this way.

             person                .GoTo(p => p.Dep.Union.Name)                .If(n => n.Length >= 5)                .Do(Console.WriteLine);

Very concise, isn't it? How can this problem be achieved? You only need to add the following extension classes:

using System;using System.Linq;using System.Linq.Expressions;using System.Reflection;namespace LinqTesting{    public static class Helper    {        class IsNullVisitor : ExpressionVisitor        {            public bool IsNull { get; private set; }            public object CurrentObject { get; set; }            protected override Expression VisitMember(MemberExpression node)            {                //it will call this overrided method with higher level node                base.VisitMember(node);                if (CheckNull())                    return node;                var member = (PropertyInfo)node.Member;                CurrentObject = member.GetValue(CurrentObject, null);                CheckNull();                return node;            }            private bool CheckNull()            {                if (CurrentObject == null)                    IsNull = true;                return IsNull;            }        }        public static TReturn GoTo<T, TReturn>(this T root, Expression<Func<T, TReturn>> funcGetValue)        {            var visitor = new IsNullVisitor();            visitor.CurrentObject = root;            visitor.Visit(funcGetValue);            if (visitor.IsNull)                return default(TReturn);            return (TReturn)visitor.CurrentObject;        }        public static TInput If<TInput>(this TInput o, Func<TInput, bool> evaluator)          where TInput : class        {            if (o == null) return null;            return evaluator(o) ? o : null;        }        public static TInput Unless<TInput>(this TInput o, Func<TInput, bool> evaluator)          where TInput : class        {            if (o == null) return null;            return evaluator(o) ? null : o;        }        public static TInput Do<TInput>(this TInput o, Action<TInput> action)        where TInput : class        {            if (o == null) return null;            action(o);            return o;        }    }}



Tips in dota

1. if you and DOOM are on the same line, no matter how cheap you have been in the early stage, please install your grandson, Tower, click farming, and move over to 6 levels, and remind your teammates as appropriate.
2. Match Phoenix. Please try to bring a large ointment.
3. match the bat. Take the magic wand as soon as possible. 2. Don't fold too much oil. 3. Don't trust your tower too much. It is also the most dangerous time for you to press your troops under your tower.
More than 4.90% of the hero Level 1 can point SF life cannot take care of themselves.
5. Some heroes can be called anti-killer kings, JUGG/LUNA/TINY. Please do not be tempted by red blood. Please kill them and ask for skill CD.
6. Never worry about how to install a teammate. It is a good teammate to work with you to kill your opponent.
7. Do not blindly follow suit. This is often the beginning of a tragedy.
8. Never use a hero to think about that set of equipment. Restraint physical DPS has a sheep knife with a green rod, restraint law system hero ziyuan, restraint consumption hero Mei Ken has a flute. At the beginning of each game, have you carefully opened the score board and watched the game against heroes?
9. As for the big moves of shadow shamans, pushing a tower in the early stage is much more useful than killing a hero. (Accumulating economic advantages in the early stage and compressing competitor space) it is more cost-effective to cooperate with teammates in playing GANK group. (After a long time for the other party's heroes to be revived and revived, killing the opponent directly becomes a situation of more attacks and fewer attacks. In the later stage, high-level anti-tower equipment is nothing)
10. About the bomber. This hero is used for playing a group, not just for Yin Ren. When you can understand it, you don't care if your opponent has any eyes or no gems.
11. When a person encounters a fish, the real eye is much more useful than the pink eye. Many times, the real eye can be inserted in the local place, and the result of dropping the powder is that you have been killed.
12. if you are a SOLO hero, especially 1V2, do not change your life with the other party (except FB ). when you change 1 to 1, the other person will have more experience and free farm in the next time.
13. Some heroes will not be afraid of you even if 1V2, such as The Lich and Lich. Maybe you or your teammates can easily take away their opponents, and when you get together, you are taken away.
14. The hero of ant has a strong ability to kill the tower. R's Summoner has A high degree of hate, which allows you to catch up with the opponent A, who is comfortable with being attacked. When using ant financial to search for R, especially when it comes to control and the outbreak of dangerous heroes, do not use C to launch common attacks immediately when it comes to people, we should keep C Security a little persistent and wait until the attack ends. CD time + competitor response time will make you more comfortable. Otherwise, you may be taken away.
15. V. S. E has two functions: 1 open field of view and 2 break a physical output. Most of the time when you use E, it is the beginning of raid. Otherwise, please do not mess up E. This is not a consumable AOE skill. Are you reminding your opponent that you are here?
16. For puck f, keep it manual most of the time, unless you need to hide the TK laser or go on the road to prevent ZEUS from attacks by hackers such as Xiaoqiang. In addition, if you see a hammer, you don't need to wait until it passes over. If you press F, you can continue the attack. The extra 1-2 seconds is enough for you to do a lot of things. The same is true for the Hidden Sand king of TA.
17. You need to be attacked to trigger your passive skills. There is A cleaning technique: In the standing army pile, any enemy hero you can see on map A is then quickly dropped (whether they are not near you ), in this way, a soldier will be seduce by you to attack you and trigger your passive skills.
18. the big promotion of juya haimin is a continuous BUFF. You can make a big promotion before rolling it with a snowball to avoid the embarrassment of getting rid of the opponent after rolling the R, especially in the early stage, the snowball level is not high enough, and vertigo is not long enough.
19. It is impossible to meet your opponent with an underdog or All-powerful power. Please be rational if you lose your skills first. A nasty hero also has an elf. If he connects to a physical DPS teammate, such as a pat on a god, what you need to do is not to fight hard with his teammates, please restrict his teammates and then the second genie. Otherwise, you will be waiting for 1V5.
20. Unlike most SOLO's heroes, the contribution period of TK is not in ...... the rest of the article>

C Language Learning Skills

This is an article I have seen before. It feels good. However, many of them are out-of-the-box online.

Programmer's path-how to learn C language and be proficient in C Language

Programmer's path-how to learn C Language
Learning C language is not an overnight task, but it does not take ten years to become proficient. The topic of this article is how to learn and master C language at minimal cost. Please note that even the "minimum price" is by no means a shortcut, but it is the shortest time to get the most, it also means that you need to go through a difficult process.

1. read a good book; otherwise, it is better not to read

The first question for all beginners is how to choose teaching materials. A good start is half the success. Choosing a good teaching material is a key factor to get twice the result with half the effort. Unfortunately, schools usually help you specify a poor C language textbook. Fortunately, you can choose again.

Professor Tan haoqiang, the famous author of C language programming, has published more than 4 million of the circulation. As far as I know, many schools recommend this book as a C language textbook. Although my name (TAN haoyu) is just a word different from that of the professor, I am very firm in hacking his book. This book is not intended for computer students, but for other students who need to take the computer level test. The main disadvantage of this book is that the example program is very unprofessional and cannot teach you the way of thinking that should be mastered in programming. The program style is rather bad and will lead you to bad habits of writing code in disorder; there are too many errors. Some people have pointed out hundreds of errors in this book, many of which are critical Conceptual errors. Well, I don't want to talk too much about this book. If you are interested, please refer to Baidu :)

The C Programming Language (Chinese translated as C Programming Language) by Kernighan and Ritchie is a classic, but many of The old versions are outdated, it is far from the current standard C language. You must look at the latest version. Otherwise, it is better not to look at it. In addition, even the most classic and authoritative books cannot cover all aspects. Therefore, it is necessary to keep a "C Language Reference Manual" at hand. The C Language Reference Manual is the C Reference Manual, which is a detailed description of the C language standard, including the details of the vast majority of C standard library functions. It is regarded as the best standard C language tool. By the way, the latest C programming language is revised in accordance with the C89 standard, and the C Language Reference Manual describes the C99 standard, which may be somewhat different, we recommend that you follow the C99 standard. Another book, "C and pointer", is also quite well written. Its English name is "Pointers on C". It emphasizes the importance of pointer, which is a special feature of this book. However, this book is not very suitable for beginners. If you have studied C language and have some basic but not very solid C language, you can try it. I believe that as long as you understand the pointer, the C language is no longer mysterious.

If you have completed a C language textbook and want to go further, you must read two books. The first is C Traps and Pitfalls, a thin booklet with interesting content. Note that this book was written more than 20 years ago. Many defects in C language have been improved, but it is not a bad thing to understand some history. Then you can challenge the Expert C Programming (Chinese Translation of "C Expert Programming"). The book is as difficult as its name. Once you finish reading it carefully, you can understand it thoroughly, you can write "proficient in C Language" on your resume with confidence.

Keep in mind that it is a waste of life instead of reading books that you cannot understand. If you cannot understand it, you must have some basic knowledge. At this time, you need to carefully analyze what you need to add, and then go to the bookstore to find the books about the content. After completing the basic knowledge, you can get twice the result with half the effort.

Ii. Unix/Linux or Windows, which is a big problem

Different programming environments will create programmers with different ideas. Most programmers in Windows rely on the integrated development environment, for example, choose a... the full text>

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.