Visual Studio tip 3 secret code samples
Tags: beginners, productivity, codenewbie, csharp
Published: 2023-08-17 21:29:44

## Introduction

At any level of expertise with C# there is always something new to learn or relearn techniques not used often. 

For this a developer should read the documentation if the class and/or method is known. If the documentation does not help let's look at a new feature in Microsoft Visual Studio 2022 (VS2022) known as **GitHub Examples and Documentation** which in many cases is in your face when hovering over many classes and methods.

## GitHub Examples and Documentation

Example, a developer wants to use [Enumerable.Range](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.range?view=net-7.0) but perhaps the documentation and/or code sample is not enough. Hover over Enumerable.**Range** and the following is shown.

![Tooltip hovering over Enumerable.Range](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ch3lb08ucah1aopsziq.png)

Single click on **Range** and press <kbd>CTRL</kbd> + <kbd>ALT</kbd> + <kbd>'</kbd> which opens a code sample window as shown below.


![Code samples window](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rywt0the1je25vxace8w.png)

- Note at top of the window there is a link to Microsoft docs
- In the top right hand corner of each sample is a button to copy code.
- Bottom left corner of the window is a button to traverse to the GitHub repository.
- Bottom right corner is a feedback button.

If a method has overloads e.g. [SqlConnection](https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection?view=dotnet-plat-ext-7.0) there is a dropdown at the top of the code window to traverse to a specific overload.

**Overload dropdown**

![Code sample overload dropdown](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3cosqsg2pklmx0zfbge6.png)

## Important for code sample

Some code samples may have code that will not compile, as in the following example were **EnsureEfTestUserExists();** will not exists in your code, in this case copy the code into Notepad, remove code like this than paste into Visual Studio.

```csharp
[Fact]
public void Can_initialize_database_when_using_secure_connection_string_with_SqlCredential_and_eager_connection()
{
    EnsureEfTestUserExists();

    var connectionString = new SqlConnectionStringBuilder()
    {
        DataSource = @".\SQLEXPRESS",
        InitialCatalog = DefaultDbName<PersistSecurityInfoContext>()
    }.ConnectionString;

    var password = new SecureString();
    foreach (var c in "Password1".ToCharArray())
    {
        password.AppendChar(c);
    }
    password.MakeReadOnly();

    var credential = new SqlCredential("EFTestUser", password);
    var connection = new SqlConnection(connectionString, credential);

    var context = new PersistSecurityInfoContext(connection, true);
    try
    {
        context.Database.Delete();
        context.Database.Initialize(force: true);
        Assert.True(context.Database.CompatibleWithModel(true));
    }
    finally
    {
        Assert.True(connection.State == ConnectionState.Closed);
        context.Database.Delete();
        context.Dispose();
    }
}
```
In the above code sample there is code for working with passwords, best practices are not to store passwords even with [SecureString](https://learn.microsoft.com/en-us/dotnet/api/system.security.securestring?view=net-7.0).

Even if code looks perfect, never simply copy and paste, make an effort to first understand the code.

## How are these code samples done?

Microsoft scanned over 100,000 GitHub repositories and looked at popular repositories. With this known, there will be some methods that indicate they have code but will not so don't think there is something wrong.

## Configure VS2022 for GitHub Examples and Documentation.

From Visual Studio menu

1. Select Tools menu
1. Options
1. IntelliCode
1. General tab
1. Ensure Enable API Usage Examples is checked


![VS2022 IntelliCode options dialog](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4yyz3bica8l3ggj8rh8.png)

## Advance developers

For developers that need minimal docs, place the mouse cursor over a class e.g. SqlConnection and press <kbd>F1</kbd> to open a browser window for, in this case SqlConnection.

Another example for a method, cursor to ExecuteReader and press <kbd>F1</kbd> to open a browser window for help.

```csharp
using var reader = cmd.ExecuteReader();
```
The above is only for Microsoft classes and methods.

## Summary
 
Knowing about **GitHub Examples and Documentation** provides an extra way to get assistance for how to work with many classes and methods if official docs are not sufficient and in many cases may save a developer from searching online blogs and developer forums for assistance.