HTML | CSS | JavaScript | ASP.NET | PHP | C# | Java | VB.NET | MS SQL | MySQL | Flash
Appreciating a good, well-designed API
I have been using the .NET Framework heavily since 2002, and sometimes I have to sit back an marvel at a good, well-designed the API. I say this because just today I had to write some code to write to the file system, and it’s remarkable how simple it is to create a text file and write information to it. You can all do it in just 2 lines of code using the System.IO.File class. Simply put, this class is a façade (or a wrapper) around the classes in the .NET Framework that work together to provide access to the file system on a Windows machine. All the methods are static (shared for VB.NET) and it provides the most common functionality that you would normally need from the file system. Here is a little code to show you what I mean:
1: using System;
2: using System.IO;
3: using System.Text;
4:
5: namespace SimpleFileWriter {
6: class Program {
7: static void Main( string[ ] args ) {
8: using (StreamWriter writer = File.CreateText( @"c:\temp\test.txt" ))
9: writer.WriteLine( "Hello World!" );
10:
11: }
12: }
13: }
I have posted the entire program just for your reference; but only lines 8 and 9 are needed to write data to the file system. You can find more information on the other members of the File class here. I’m sure this may not be new to some of you; but the intent of this post was to emphasize how a well-designed API goes a long way. Until next time…
Happy Coding!
| Print article | This entry was posted by admin on December 11, 2007 at 3:46 pm, and is filed under .NET Framework. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 2 years ago
Here’s the entire program in Ruby (and I assume Ruby.NET too)
open(‘c:\temp\test.txt’,'w’).puts “Hello World”
about 2 years ago
Wow! Dynamic languages have a tendency to do alot of things under the cover for you. Maybe C# 4.0 will make this even more simple like it is in Ruby. Thanks for sharing this.