Getting Started with LINQPad

This guide includes examples that will teach you how to begin using LINQPad for testing C# code snippets and programs. You will learn why LINQPad is a useful tool to integrate into your C# development workflow.

Have you ever found yourself having to fire up a new instance of Visual Studio and then create a new project just to test a simple C# statement? With LINQPad, can test C# expressions, database queries, and even full programs, in a lightweight code editor. It can be a great timer-saver in your development workflow.

To get started, download the latest version of LINQPad from their website. I am using LINQPad 6, because it is designed to work with .NET Core.

LINQPad for Simple Expressions

Suppose you were walking through my C# for beginners tutorial series, and you were ready to experiment with using a foreach loop to loop through the elements of an array.

You could simply open LINQPad and paste the following snippet as a test program.

string[] languages = new string[] { "English", "French", "Portuguese", "Spanish" };
foreach (string language in languages)
{
    Console.WriteLine(language);
}

Now, when you execute the query, by pressing F5 on the keyboard, you will see the output printed to the Results pane.

LINQPad for Console Applications

LINQPad obviously works great for testing in simple scenarios where you may want to see certain values printed to the console. However, suppose you want to test a full console program where the user must interact with your application.

With LINQPad, you can still use the methods of the Console class that you are already used to, such as Write(), WriteLine(), Read() and ReadLine(). Consider the following example.

Console.Write("What is your name: ");
string myName;
myName = Console.ReadLine();
Console.WriteLine();
Console.WriteLine($"Hello, {myName}");

LINQPad with Multiple Methods

Recall, when creating a console application, your program starts up by executing the code in the static void Main() method. With LINQPad, you don’t usually have to worry about this detail, because the code in the scratchpad will automatically execute.

However, what if you want to include other explicitly defined methods or classes? Such scenarios are easy to accommodate by simply setting Language to C# Program in the LINQPad editor.

Doing so will generate a template like the following:

void Main()
{
 
}
 
// Define other methods, classes and namespaces here

Like with console applications in Visual Studio, the code in the void Main() method will execute on startup. You are free to define other methods and reference them throughout your program.

If you were to recreate the program from the C# Reverse String tutorial in LINQPad, you would end up with something similar to the following screenshot.

LINQPad with Multiple Classes

Not only can LINQPad be used with multiple methods, but it can also be used to test programs with multiple classes.

In the Introducing C# Collections tutorial, you created an application to manage the books you might have in a library. This program depended on a custom Book class to store such information as the title, author, and publication date.

class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    public int PubDate { get; set; }
 
    public Book(string title, string author, int pubDate)
    {
        Title = title;
        Author = author;
        PubDate = pubDate;
    }
}

With LINQPad in C# Program mode, you can define this class and then reference it from the void Main() startup method. As you can see, LINQPad isn’t limited to simple expressions or statements, it can be used to test more complex solutions with multiple methods and classes.

LINQPad with LINQ Queries

Finally, it is worth giving an example that demonstrates LINQPad’s ability to evaluate LINQ queries. Recall that LINQ (Language-Integrated Query) can be used to retrieve data from any source by using a uniform syntax involving lambda expressions.

This example follows the LINQ syntax tutorial about filtering and sorting C# lists.

The first thing to note is that it is not necessary to add a using statement to reference the System.Linq namespace. Simply begin writing your code and start using LINQ expressions.

You may have also noticed in the previous example that you were able to declare and instantiate a list of books, List<Book>, without the need to add a using reference for the System.Collections.Generic namespace. Because LINQ and generic lists are both such common usage scenarios, LINQPad imports them automatically.

You can modify the list imported namespaces via the Query > Namespace Imports menu. By default, LINQPad automatically imports the following references:

System
System.Collections
System.Collections.Generic
System.Data
System.Diagnostics
System.IO
System.Linq
System.Linq.Expressions
System.Reflection
System.Text
System.Text.RegularExpressions
System.Threading
System.Transactions
System.Xml
System.Xml.Linq
System.Xml.XPath

The Bottom Line

In this tutorial, you learned how LINQPad can be used to save you time when developing and testing C# programs. You walked through examples ranging from testing simple statements to more complex programs with multiple methods and classes. You also saw how LINQPad can be used to work with and test features involving collections and LINQ queries.

Not only is the LINQPad coding environment lightweight and responsive, but compile times are significantly reduced when compared to a full build and deployment. This allows you to iterate more quickly and be more productive.


Was this article helpful? Share it with your friends:

Share on email
Share on facebook
Share on twitter
Share on reddit

Leave a Comment

Looking for more?

You have visited our site before, and we appreciate you!

If you found the tutorials helpful, enter your email address for more free C# tips and tricks.

Can't get enough C#?

Enter your email address for more free C# tutorials and tips.