Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cost of Referencing a library for just one use

Tags:

c#

.net

vb.net

I know this is a truly weak example, but my question is more for understanding better programming practices, so please bear with me for the example itself.

In working on a program, I have a situation where I have to replace multiple characters in a string with a single, different character.

For example, all the characters in {" ", "-", "/", "*"} must all be replaced with an underscore "_". But this only happens once in the entire program and the result is stored in a variable.

So, I know I could do it using multiple String.Replace() statements in the following way:

string ChangedString1 = MyString.Replace(" ", "_").Replace("-", "_").Replace("/", "_").Replace("*", "_");

But I know I could also do it using a regular expression such as:

using System.Text.RegularExpressions;
   ...
string ChangedString2 = Regex.Replace(MyString, @"[\s-/*]", "_");

Now, my question isn't about which method is better, rather the question is how costly is it to include the using System.Text.RegularExpressions; statement for only one use such as this? And, perhaps System.Text isn't that expensive, but what about adding in other references for just one small use?

The question is if there is some kind of a industry preference regarding at what point is it better to write your own method rather than including a reference to a library just for one or two of its smaller functions. OR am I mis-understanding how references work and are they not that expensive in the first place?

Thanks for your help and I hope this question makes sense!

like image 882
John Bustos Avatar asked Jan 03 '14 15:01

John Bustos


1 Answers

using statements are a purely compile-time feature that let you omit the namespace when referencing a class.
They have no effect at runtime.

Those classes are in mscorlib.dll, which you're referencing anyway.

like image 120
SLaks Avatar answered Sep 22 '22 17:09

SLaks