Is it a good practice to declare variable using the interface? In my company we had a discussion on it and I'm quite against it.
e.g. I wanted to have a collection storing string keys and string value pairs. I don't want to allow duplicates either. So clearly I declared Dictionary variable in my class which would be exposed publicly (via property).
Dictionary<string, string> myDic;
However, a team member says that it's not a good practice ! He is saying that you declare a variable with IDictionary and this will allow consumers to assign any collection (implementing IDictionary) which they want. e.g. Hashtable or Dictionary
IDictionary myDic;
myDic = new Hashtable(); // Consumer's code
OR
mydic = new Dictionary<string, string>(); // Consumer's code
-
May I know now, is it really a good practice to declare variable as type of interface? And that too when I clearly know what is expected from that variable?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…