Repository navigation
Expand file tree
/
Copy path2008-04-20-extension-method-just-because-i-can.html
More file actions
13 lines (11 loc) · 3.58 KB
/
Copy path2008-04-20-extension-method-just-because-i-can.html
File metadata and controls
13 lines (11 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
---
layout: post
title: An extension method, just because I can.
date: '2008-04-20T12:43:00.001+01:00'
tags: [patterns, csharp]
modified_time: '2008-04-20T12:43:34.660+01:00'
blogger_id: tag:blogger.com,1999:blog-4015568221071268916.post-5524975972244453378
comments: true
blogger_orig_url: http://serialseb.blogspot.com/2008/04/extension-method-just-because-i-can.html
---
<p>Going to the dentist is an awkward experience. You know you have to use their service or your teeth will rot, but the experience is just not enjoyable.</p> <p>That's how I feel about string.Format. I use it all over the place, but find it incredibly out of place. Thanks to extension methods though, I can propose a nicer syntax. Here's a snippet from a site I'm building for one of my clients, using my rasta REST framework and linq2sql.</p> <div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new"> <div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new"> <p style="margin: 0px">[<span style="color: #2b91af">HttpOperation</span>(<span style="color: #2b91af">HttpMethod</span>.GET, ContentType=<span style="color: blue">new</span> <span style="color: #2b91af">HttpContentType</span>(<span style="color: #a31515">"image/*"</span>))]</p> <p style="margin: 0px"><span style="color: blue">public</span> <span style="color: #2b91af">ResourceOperationResult</span> GetMovie(<span style="color: blue">string</span> filmName)</p> <p style="margin: 0px">{</p> <p style="margin: 0px">    <span style="color: blue">var</span> movie = _movies.FindOne(movie => movie.UrlTitle == filmName);</p> <p style="margin: 0px"> </p> <p style="margin: 0px">    <span style="color: blue">if</span> (movie == <span style="color: blue">null</span>)</p> <p style="margin: 0px">        <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ResourceNotFoundException</span> </p> <p style="margin: 0px">        {</p> <p style="margin: 0px">            Message = <span style="color: #a31515">"The movie {0} wasn't found in the database."</span>.With(filmName) </p> <p style="margin: 0px">        };</p> <p style="margin: 0px"> </p> <p style="margin: 0px">    <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">ResourceMovedTemporarilyResult</span> { NewResourceUrl=<span style="color: #a31515">"/library/tempfilm.jpg"</span> };</p> <p style="margin: 0px">}</p> </div> </div> <p>Notice the extension method there? I just find it sweet. Here's the very simple extension method.</p> <div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new"> <p style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">string</span> With(<span style="color: blue">this</span> <span style="color: blue">string</span> text, <span style="color: blue">params</span> <span style="color: blue">object</span>[] values)</p> <p style="margin: 0px">{</p> <p style="margin: 0px">    <span style="color: blue">return</span> <span style="color: blue">string</span>.Format(text, values);</p> <p style="margin: 0px">}</p> </div> <p>There you are, a shamefully simple extension method, just because I can!</p>