Back when Dinosaurs ruled the earth, developers had these things called API references. They were physical books (made of paper) which contained thousands upon thousands of pages of nothing but documentation for APIs. If you wanted to do something, and you didn’t know how, you either asked the wise old developer that was working with you or you looked up the answer in the API reference. Thankfully I never had to experience that, I mean seriously, how old do you think I am? But what I do remember is the bad old days when search engines weren’t very programming language friendly and you couldn’t even search for anything with special symbols in it because all of the search engines just stripped it all out! Searching for code was quite hard!
Once Google came along things changed. Not only was Google very friendly to programming languages, but it actually returned relevant results to our queries. Over time, the amount of online programming info flourished, and all was good. Well, all was good if you were to leverage the tools that you have at your disposal. And let me be clear, Google was the single most powerful development tool ever created, and probably still is (insert Bing joke here).
So I was plowing through reams of code today (I wish) and I came across a place where someone was manually parsing a connection string in order to extract its contents. I chuckled a bit (you gotta stay light hearted about some of this stuff) and then I went to Google and typed in "C# Parse Connection String". Hmmm, that first answer looked pretty darn good. The first result was the MSDN API reference for SqlConnectionStringBuilder… which turned out to be exactly what I was looking for. Pretty sweet. And to think, a class exists to parse a connection string! Who would have thought? Well, I for one, and I hope that you would think that too. This is what I like to call "a well known problem". It is a problem that a bajillion other people are almost certainly to have come across in their programming adventures, and so someone else has most likely solved it, and probably posted the answer online! Just in case you found this while searching for connection string parsing in C#, I’ll post the answer here:
var sqlConnectionStringBuilder = new SqlConnectionStringBuilder(); sqlConnectionStringBuilder.ConnectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"; Console.WriteLine(sqlConnectionStringBuilder.InitialCatalog); Console.WriteLine(sqlConnectionStringBuilder.DataSource); Console.WriteLine(sqlConnectionStringBuilder.UserID);
The funny thing about this is that just last night I was chatting (literally, in a chat program) with my buddy Dave Ward and this exact topic came up. How come people don’t do a simple Google search whenever they come to a well known problem? His comment was "How many times has Path.Combine been rewritten?" He has an excellent point, it has probably been rewritten about a million times. And it is a complete waste of time. Although Path.Combine does have a few funky behaviors that could probably be improved upon Don’t believe me? Try doing this:
But anyways… whenever you come across a problem like say combining paths, parsing a connection string (or a query string!), or a csv file, make sure you do a few quick searches before you decide to write code to do your own. Sure it may seem easy to parse a connection string, but what happens when you are looking for "Default Catalog" in the connection string and someone throws in a connection string that uses "Database" instead? I bet the framework parser will account for that, but unless you spent your time and due diligence, yours probably does not.
So I implore you, get out there and do a search before you give up and write code…You’d be amazed at what you’ll find lurking in the .NET framework, and the world of code that exists out there in the open source community. I’ll leave you with a few quick examples of what you might find just in the .net framework:
- Parse connection string: System.Data.SqlClient.SqlConnectionStringBuilder
- Parse query string: System.Web.HttpUtility.ParseQueryString
- Parse CSV file: Microsoft.VisualBasic.FileIO.TextFieldParser
- Generate Temporary File Name: System.IO.Path.GetTempFileName
- Time Code: System.Diagnostics.Stopwatch: stop getting the current time and trying to subtract it. This has a much higher precision!
- Coerce Types: System.ComponentModel.TypeConverter
- Check if a string is null or empty: String.IsNullOrEmpty
- Parse Any Uri: System.Uri
- Basic Data Structures: System.Collections.Generic.LinkedList, Queue, Stack: I’ve seen people try to emulate these with lists!
- Add list of items to another list: System.Collections.Generic.List.AddRange: Don’t loop through another list adding items!
- Watch for changes in a file system: System.IO.FileSystemWatcher
There are probably a ton more examples of these, but I’ll let you explore a bit more and let me know which ones you find! So get out there, use Google, Yahoo, Bing, Ask, etc… and find some code that you can reuse!
Loved the article? Hated it? Didn’t even read it?
We’d love to hear from you.