10/12/2016 - 6:36 PM

Tips and Traps

Tips and Traps

1. You can override the debug information - instead of seeing the default namespace, you can customize the message. This can be done by override the ToString() method or by adding DebuggerDisplay attribute at the class/member level

2. You can hide/modify the value of the members with DebuggerBrowsable attribute

3. You can get the called property name/ caller code line or caller file path with adding attribute to the method parameters like this public void Test([CallerLineNumber]string ceva=null).
If we call the Test method without any parameters, the ceva parameter will be instantiated with the line number

4. var x="Hello"; var y="Hello"; var isTheSameReference = object.ReferenceEqual(a, b) will be true because compiller checks if the values of these variables are equal and share the location in case is true even if the string is a reference type.

5. In case when we want to use multiple choises for an enum we have to add [Flags] attribute and use | for multiple choise.
E.g: var color = ColorEnum.Blue | ColorEnum.Yellow;
To check if a color is present we can do var isBluePresent = (colo & ColorEnum.Blue)!=0;
To remove or add one of the color from the enum: color^=EnumColor.Red

5. You can customize the format of the numbers on display with number format options:
            var a = 99.22;
            var b = -22.26;
            var c = 0;
            //is not important what you write here, since first part until ; is for positive numbers, second for negative and the last one for 0
            var numberFormat = "(+)#.##;(-)#.#;(nothing)";

            Console.WriteLine(a.ToString(numberFormat));//displays (+)99.22
            Console.WriteLine(b.ToString(numberFormat));//displays (-)22.3
            Console.WriteLine(c.ToString(numberFormat));//displays (nothing)

6. You can declare a method/class obsolete with the [Obsolete] attribute and the cosumeers will get an warning when they'll use it.

7. You can add a file notification handler for a folder

var watcher = new System.IO.FileSystemWatcher();

watcher.Changed += FileChanged;

8. In case you have a method that returns a IEnumerable, you can call an operators to force the method to run.
.ToList(), Count(), etc - these operators are called greedy operators.

9. To get distict elements of a object list with linq, you cannot use just Distinct() because this is checking just the object reference.
Instead, what you can do is to use anonymous objects because they have a custom implementation of the Equals method ant they check every property
var books = new List<Books>
  new Book{Author="Ion", Name=".NET"},
  new Book{Author="John", Name=Java"},
  new Book{Author="Ion", Name=".NET"},

var query = books.Distinct() will return 3 elements
var query = books.Select(b=>new {b.Name, b.Author}).Distinct() will return 2 elements.

10. In case you want to validate an object properties, you can create a list of rules and check them all with All statement
var bookValidationRules = new List<Func<Book, bool>>()

bool isBookValid = bookValidationRules.All(rule=>rule(book));

One more cool thing that we can do is to create a generic class that can be used for multiple classes:
public class Rule<T>
  public Funct<T, bool> Test{get;set;}
  public string Message{get;set;}

Employee employee = new Employee();

var rules = new List<Rule<Employee>>()
  new Rule<Employee> {Test = e=> !string.isnullorempty(e.Name)
                      Message = "Emnployee name cannot be empty"},
  new Rule<Employee> {Test = e=>e.Id>0,
                      Message = "ID should be greater than 0"}

var isValid = rules.All(e=>e.Test(employee));
var allBrokenRulesMessages = rules.Where(e=>!e.Test(employee)).Message;