Increase Productivity with LINQPad
Tags: csharp, dotnet, productivity, beginners
Published: 2019-11-26 12:34:47

---
title: Increase Productivity with LINQPad
published: true
tags: csharp, dotnet, productivity, beginners
canonical_url: https://wellsb.com/csharp/learn/getting-started-with-linqpad/
---

[Original Article](https://wellsb.com/csharp/learn/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](https://www.linqpad.net/Download.aspx). 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](https://wellsb.com/csharp/tutorials/beginners/), and you were ready to experiment with using a foreach loop to [loop through the elements of an array](https://wellsb.com/csharp/beginners/csharp-foreach-loop-through-array/).

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

```csharp
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.

[![](https://wellsb.com/csharp/wp-content/uploads/sites/2/2019/11/linqpad-writeline-min-300x169.png)](https://wellsb.com/csharp/wp-content/uploads/sites/2/2019/11/linqpad-writeline-min.png)

## 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](https://wellsb.com/csharp/beginners/get-user-input-in-csharp-with-console-readline/).

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.

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

[![](https://wellsb.com/csharp/wp-content/uploads/sites/2/2019/11/linqpad-readline-min-300x169.png)](https://wellsb.com/csharp/wp-content/uploads/sites/2/2019/11/linqpad-readline-min.png)

## 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:

```csharp
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](https://wellsb.com/csharp/beginners/string-manipulation-reverse-string-csharp/) tutorial in LINQPad, you would end up with something similar to the following screenshot.

[![](https://wellsb.com/csharp/wp-content/uploads/sites/2/2019/11/linqpad-methods-min-300x263.png)](https://wellsb.com/csharp/wp-content/uploads/sites/2/2019/11/linqpad-methods-min.png)

## 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](https://wellsb.com/csharp/beginners/csharp-initialize-list/) 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.

```csharp
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.

[![](https://wellsb.com/csharp/wp-content/uploads/sites/2/2019/11/linqpad-classes-min-300x224.png)](https://wellsb.com/csharp/wp-content/uploads/sites/2/2019/11/linqpad-classes-min.png)

## LINQPad with LINQ Queries

Finally, it is worth giving an example that demonstrates LINQPad’s ability to evaluate LINQ queries. Recall that **LINQ** ( **L** anguage- **In** tegrated **Q** uery) 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](https://wellsb.com/csharp/beginners/linq-syntax-filter-list/).

[![](https://wellsb.com/csharp/wp-content/uploads/sites/2/2019/11/linqpad-linq-min-300x205.png)](https://wellsb.com/csharp/wp-content/uploads/sites/2/2019/11/linqpad-linq-min.png)

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:

```csharp
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.

[Source](https://wellsb.com/csharp/learn/getting-started-with-linqpad/)