In C# when using the ref keyword it causes an argument to be passed by reference, not by value. In order to use a ref as a parameter, both the method definition and the calling method must explicitly use the ref keyword, and also the variable must be initialized before passing in. In the example […]
.NET provides in its System.IO namespace the Path class which performs operations on String instances that contain file or directory path information. These operations are performed in a cross-platform manner. Most of the time we see develeopers writing code like
|
1 |
string path = somePath + "\\" + filename; |
But by using Path.Combine we can provide a cross platform path
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
string somePath = @"C:\temp"; string filename = "dat.txt"; string path = Path.Combine(somePath,filename); //produce an output of C:\temp\dat.txt //linux: C:/temp/dat.txt string[] paths = {@"c:\My Music", "2013", "media", "banner"}; string fullPath = Path.Combine(paths); //output c:\My Music\2013\media\banner string path1 = @"C:\Temp"; string path2 = "My Music"; fullPath = Path.Combine(path1, path2); //output: C:\Temp\My Music fullPath = Path.Combine(string.Empty, path2); //output: My Music fullPath = Path.Combine(path1, string.Empty); //output: C:\Temp |
By using […]
Recently have been going through some old code to review the comments in them from other developers and what I find out is developers tend to have really bad comments & documentation in their code Example:
|
1 2 3 4 5 6 7 8 |
public class Person { /// <summary> /// Constructor /// </summary> public Person() {} } |
From the above code it is obvious that it is the constructor but does the comment tell me […]
Here is a LINQ tip where you may wish to order a collection with an existing ordering of another collection. Example:
|
1 2 3 4 5 6 7 8 9 |
int[] displaySeq = new int[] { 1, 8, 5, 7, 13 }; //the ordering we want at the end //collection that we will be sorting on List<Person> people = new List<Person>(); people.Add(new Person { DisplaySeq = 5}); people.Add(new Person { DisplaySeq = 13}); people.Add(new Person { DisplaySeq = 7}); people.Add(new Person { DisplaySeq = 1}); people.Add(new Person { DisplaySeq = 8}); |
Currently as it stands our data is stored in the order of { 5, 13, 7, 1, 8 } and we wish to order them in terms of { 1, 8, 5, 7, […]
Obsolete or Deprecated? How do you mark a class or method as deprecated/obsolete? By using the Obsolete attribute
|
1 2 3 4 5 6 7 8 9 10 11 12 |
[Obsolete] public class Person { } [Obsolete("Class is obsolete, use PersonImpl")] public class Person { } //compilation to fail methods calling this code [Obsolete("Class is obsolete, use PersonImpl", true)] public class Person { } [Obsolete("Method is obsolete, use SomeMethod2", true)] public void SomeMethod() { } |
The ?? operator is called the null coalescing operator. It is used for providing a default value for Nullable types or reference types. Example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
int? age = null; //what ?? implies is possiblyNullValue ?? valueIfNull int myAge = x ?? 100; //if x is not null then assign x else 100 string? person = null; string? localDefault = null; string globalDefault = "abc"; string anybody = person ?? localDefault ?? globalDefault; //chaining ?? //lazy loading/populating private SomeObj _lazyField = null; public SomeObj MyProperty { get { return _lazyField ?? (_lazyField = new SomeObj()); } } |
One of the disadvantage of ?? is it can create code that is not that readable. e.g a ?? b ?? c ?? d ?? e
In C# you can have property with different scope (property accessors). Properties in C# can be marked as public, private, protected, internal, or protected internal. Example:
|
1 2 3 4 5 6 7 8 |
public int MyProperty { get; private set; } public string Name { get; protected set;} public int Age { get { return _age; } } // no set but still valid private string Fullname { get; set; } //both are private only class can access it internal string LName { get; set; } //only code in same assembly can access protected internal string FName { get; set; } //only code that inherit and in same assembly virtual string PersonName { get; set; } //use override keyword in derived class static int Count { get; set; } // avaiable to caller at any time |
C# 4.0 allows one to use named and optional parameters. Example:
|
1 2 3 4 5 6 |
int Calculate(int x = 1, int y = 2) { int someNumber = 0; //some algorithm to calculate return someNumber * y * x + 5 * 0.5; } |
One can call the method in multiple ways
|
1 2 3 4 5 6 7 |
int result = Calculate(1, 2); int result = Calculate(5); //value for x only, y is default to 2 int result = Calculate(); //value for x is 1 and y is 2 int result = Calculate(x: 123, y: 987); //named argument int result = Calculate(y: 345 x: 678); //named argument in reverse int result = Calculate(x: 123, 456); //compiler error not allowed position argument cannot follow name argument int result = Calculate(1, y: 456); //name argument can follow position argument |
Additional note, one cannot provide a gap in arguments. Example:
|
1 2 3 4 5 6 |
int Calculate(int x, int y = 2, int z) //invalid int Calculate(int x, int y = 1, int z= 5) //valid int result = Calculate(1, , 3); //invalid int result = Calculate(1, 8); //z takes the value of 5 as default |
The using statement in C# is a form of shortcut for try and finally block of code. Things to note is in order to use the using statement the object needs to implement the IDisposable interface, and using does not catch Exception, it just guarantees the call of Dispose. Example:
|
1 2 3 4 5 6 7 |
try { connection = new SqlConnection(""); } finally { if(connection != null) connection.Close(); } |
Can be replaced with […]
In interviews there is always the Singleton question, I personally refuse to answer it on interview. The reason is very simple I don’t code singletons anymore. I use my IoC Container to provide me with a singleton. Sorry if you dont know what an IoC (Inversion of Control Container) is, then you are still in […]

