Visual Studio tip 3 secret code samples
Karen Payne
Posted on August 17, 2023
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 but perhaps the documentation and/or code sample is not enough. Hover over Enumerable.Range and the following is shown.
Single click on Range and press CTRL + ALT + ' which opens a code sample window as shown below.
- 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 there is a dropdown at the top of the code window to traverse to a specific overload.
Overload dropdown
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.
[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.
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
- Select Tools menu
- Options
- IntelliCode
- General tab
- Ensure Enable API Usage Examples is checked
Advance developers
For developers that need minimal docs, place the mouse cursor over a class e.g. SqlConnection and press F1 to open a browser window for, in this case SqlConnection.
Another example for a method, cursor to ExecuteReader and press F1 to open a browser window for help.
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.
Posted on August 17, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.