Karl Gjertsen

Architect, Developer, Speaker, Bad Golfer,
Raygun brand ambassador and creator of Infiniforms

C#

C# coding

C#: Adding Shorts

Have you ever tried adding 2 shorts together? Did you know that a = a + b and a += b are different? I didn’t! A short is an alias of Int16, or a number that fits into 16 bits. This is half the size of an integer, which is an alias for Int32. short.MinValue = -32768 short.MaxValue = 32767…

String Interpolation in C# 6

Unlike some of the previous releases C# 6.0 isn’t a radical change from the current version of C#. But, it may still change the way you write code. A great example of this is String Interpolation. We originally started concatenating strings using the plus symbol: var myClass = new MyClass(); var fullName = “Full name: ” + myClass.FirstName + myClass.LastName; We then moved on to using String.Format: var myClass = new MyClass(); var fullName = String.Format(“full name: {0} {1}”, myClass.FirstName, myClass.LastName); Now, C# 6.0 gives us…

C#: When to use String vs string

This seems to be a common question from people who are new to programming in C#. The answer is that it doesn’t matter which you use as string is an alias for System.String. However, the convention is to use ‘string’ when you are referring to an object and ‘String’ when you are referring specifically to the string class. Example of…

Unit testing internal methods

TDD means that we do not write any code without a test, then we move code into internal, private or protected methods as we refactor. But what if we are writing a method that is to be used by other developers in our development team and it is meant to be an internal class? What happens if we are not…

Saving an email to SQL Server with Mail4Net

There are a number of reasons why you might want to save your email to a SQL Server database. Maybe you want to be able to resend the message if it doesn’t arrive. A common reason is to have the sending process performed by a dedicated service, such as MailSender, which will soon be available to download from the Mail4Net…

Inversion of control in Mail4Net

Inversion of Control All of the Mail4Net client classes implement a common interface, ISmtpClient. This allows you to use the inversion of control pattern with the Mail4Net clients. To demonstrate using IoC with Mail4Net, I have created a sample class called MailCreator class, which has a SendEmail method that takes an email address and then sends an email to that…

Unit testing email code with Mail4Net

When it comes to unit testing your code, Mail4Net provides a great way to test your email sending code. In my previous article, I showed sending an email with Mail4Net. If you are new to Mail4Net, then I would suggest you start with this article. Using the fake client The fake client stores emails that have been sent and they…

Sending an email with Mail4Net

Mail4Net is a .NET library for creating, managing and sending emails. Sending an email Typically, you create a MailMessage class, populate it and then send it to a SMTP server, using Microsoft’s System.Net.Mail.SmtpClient class: private void SendEmail() { // Create mail message. var message = new System.Net.Mail.MailMessage(); message.To.Add(“someone@mydomain.com”); message.From = new MailAddress(“me@mydomain.com”); message.Subject = “Hello World”; message.Body = “Here is…