Monads And Their Applications in C#

If you have ever written code in most modern programming languages and even languages that are not functional in nature there is a very high probability that you have used some form of monadic structure. A monad is a structure that represents computations defined as sequences of steps: a type with a monad structure defines what it means to chain operations, or nest functions of that type together. This allows the programmer to build pipelines that process data in steps, in which each action is decorated with additional processing rules provided by the monad. As such, monads have been described as “programmable semicolons”.

Monads allow you to do things like method chaining, and flattening null and exception checks in highly nested code blocks.

Monadic Rules:

1.  Left identity
Identity.Compose(f) = f

2.  Right identity
f.Compose(Identity) = f

3.  Associative
f.Compose(g.Compose(h)) = (f.Compose(g)).Compose(h)

Example: Very basic Monad to factor out division by zero check in BMI calculation.

Monads are awesome, and I still have a lot to learn about them, however I can already see them everywhere in C#: IEnumerable, JQuery: Ajax Requests and lots more.

Until next time keep learning 🙂



How To Unit Test A LINQPad Code Snippet

Writing code is a programmers life; sometimes it becomes necessary to write pieces of code that you can conveniently run and evaluate  without spinning up a full fledged IDE. For those tasks there is a tool called LINQPad. LINQPad allows you to write/run snippets in C#, SQL and a few other languages. In oder to run the example in this tutorial you will need to download LINQPad and NUnit Lite.

After installing LINQPad and NUnit Lite open an instance of LINQPad and change the language to C# Program.


Next hit the F4 key on you keyboard to bring up the additional references dialog and browse to the location where you installed NUnit Lite. You will need to add NUnit Lite as a reference.


Copy preceding code into the LINQPad edit window then hit the run button, the results of the test will be displayed in the console window, enjoy 🙂

Generating C# Classes from WebEx Request XML

Just recently I worked on a project that required me to consume some WebEx services, after the first look at some sample code I  decided not to go the route of building XML strings whenever there was a need to communicate with the WebEx servers. I wanted the ability to serialize and deserialize xml requests and responses, so here is what I did to accomplish this in C# and xsd.exe.

Sample WebEx xml request: Authenticate user

<?xml version="1.0" encoding="ISO-8859-1"?>
<serv:message xmlns:xsi=""
        <bodyContent xsi:type="java:com.webex.service.binding.user.AuthenticateUser">
                message will go here
  1. Copy the XML string to notepad and save as helper.xml
  2. At the developer command prompt type cd\ then press Enter\Return
  3. Type the path to helper.xml then press Enter\Return
  4. At the command prompt type xsd helper.xml then press Enter\Return [This will generate the helper.xsd file]
  5. Next type xsd helper.xsd /c /f then press Enter\Return  [Will generate C# classes based on the helper.xsd file]

Now lets see how we can manually load these generated classes then serialize them to XML for posting to the WebEx REST API Servers.

private void TestSerialization()
 var msgSecurityContext = new messageHeaderSecurityContext()
 email = "",
 partnerID = "webexpartner",
 password = "pass",
 siteName = "ian0830",
 webExID = "siteadmin"

var msgBodyContent = new messageBodyBodyContent() { samlResponse = "samlResponse message will go here" };

var header = new messageHeader() { securityContext = new messageHeaderSecurityContext[] { msgSecurityContext } };
var body = new messageBody() { bodyContent = new messageBodyBodyContent[] { msgBodyContent } };

var msg = new message() { Items = new object[] { header, body } };

var xmlns = new System.Xml.Serialization.XmlSerializerNamespaces();

xmlns.Add("serv", "");
xmlns.Add("xsi", "");

System.Xml.Serialization.XmlSerializer searializer = new System.Xml.Serialization.XmlSerializer(msg.GetType());

searializer.Serialize(Console.Out, msg, xmlns);

This produces an identical XML string as the one shown above.

Convert RGB To Integer In C#

If you ever wanted to convert an RGB value to an integer value, this is how you do it.
First strip the leading pound sign(#) from the string using the Substring function.

   string val = "#060810";
   var    rgb = val.Substring(1, val.Length -1);

Next split the string into 3 different parts representing red, green, and blue

   var r = int.Parse(rgb.Substring(1, 2), NumberStyles.HexNumber);
   var g = int.Parse(rgb.Substring(3, 2), NumberStyles.HexNumber);
   var b = int.Parse(rgb.Substring(5, 2), NumberStyles.HexNumber);

*NB: The int.Parse function along with NumberStyle.HexNumber is used to convert each Hex number to its equivalent decimal representation, happy coding 🙂