Skip to content

CtcApi.Extensions

Shawn (work acct) edited this page Jun 8, 2013 · 2 revisions

Table of Contents

Extension methods

Current versions of the .NET Framework allow the creation of extension methods, which can add additional methods to existing objects. Most of the method calls associated with LINQ (e.g. Where(), Select(), etc.) are implemented in the Base Class Library as extension methods. In order to use these extension methods in your code, you only need a reference to the CtcApi and the CtcApi.Extensions namespace - once that is done, the methods will automatically become available as a member of their associated objects.

Examples

String methods

.TitleCase()

This method applies title- (or camel-) casing to the string. This is useful for titles, names, etc.

 using CtcApi.Extensions;
 ...
 string courseTitle = "INTRODUCTION TO ENGLISH";
 string casedCourseTitle = courseTitle.TitleCase();

 // casedCourseTitle == "Introduction To English";

.Mash()

The Mash method is the counter to the Split method - it takes two or more strings and appends them together, separated by commas (,) or a specified separator string.

 using CtcApi.Extensions;
 ...
 string[] groups = new string[] {"Developers", "Administrators", "Users"};
 string output = groups.Mash();

 // output == "Developers,Administrators,Users";

.CombinePath()

The Path class in the .NET Framework Class Library includes a Combine() method, but it includes some unwanted behaviors in certain situations: If any of the parts being combined begins with a leading path separator (\) then everything that came before is ignored and it treats the new path as absolute - beginning with that part. CombinePath(), on the other hand, simply appends the path parts - stripping any leading path separators where appropriate.

 using CtcApi.Extensions;
 ...
 string base = "C:\basefolder";
 string appFolder = "\Program Files\AppFolder";
 string configFolder = "config";

 string pathCombine = Path.Combine(base, appFolder, configFolder);
 string apiCombinePath = base.CombinePath(appFolder, configFolder);

 // pathCombine == "\Program Files\AppFolder\config";
 // apiCombinePath == "C:\basefolder\Program Files\AppFolder\config";

.UriCombine()

The UriCombine method provides the same basic functionality as CombinePath(), but for strings that are Uniform Resource Identifiers (URIs).

 using CtcApi.Extensions;
 ...
 string uriServer = "codemastershawn.com";
 string uriPath = "blog";

 string uri = uriServer.UriCombine(uriPath);

 // uri == "codemastershawn.com/blog";

.ToFile()

The ToFile method writes the string out to the specified filename. (NOTE: This method makes use of the Byte[].ToFile() extension method below.)

 using CtcApi.Extensions;
 ...
 string fileContents = "File contents";

 if (fileContents.ToFile(@"C:\testfile"))
 {
   // success!
 }
 else
 {
   // writing file failed
 }

Byte methods

.ToFile()

This method adds a the ability to write an array of bytes out to a file.

 using System.Text;  // provides access to the Encoding class
 using CtcApi.Extensions;
 ...
 string fileContents = "File contents";
 byte[] data = Encoding.UTF8.GetBytes(fileContents);

 if (data.ToFile(@"C:\testfile"))
 {
   // success!
 }
 else
 {
   // writing file failed
 }

List methods

.AddRangeIfNotNull()

Attempting to add an IEnumerable range that is null to a List collection will result in an Exception being thrown. This method safely attempts to add the range by first checking to make sure it is not null before calling List.AddRange().

 using System.Text;  // provides access to the Encoding class
 using CtcApi.Extensions;
 ...
 List<string> list = new List<string>();
 IEnumerable<string> range = new List<string>();

 list.AddRangeIfNotNull(range);
 ...