2
头图

The first phase of the .NET 20th Anniversary Learning Challenge has just ended. Have you participated? Some people ask if learning C# is a bit outdated now? Some people also ask what can C# do now? Some people even asked if learning C# can find a job? Or you can have different answers from different experts and different practitioners. But I am more concerned with a relatively basic question that more people want to answer - how to learn C# well. I'm a .NET soldier, and while my tech stack continues to expand, I've never given up on C#. I hope to use this article to share some C# learning experience with you.

learning environment

Early learning C#, I believe everyone likes to install Visual Studio on Windows or install MonoDevelop IDE under macOS / Linux based on mono. However, after .NET is open source, learning C# can not only be done on Windows, but also on macOS / Linux. In addition to Visual Studio, the development environment also adds Visual Studio Code, as well as Rider from JetBrains, a third party. IDE etc. In fact, besides these IDEs for learning programming languages, I suggest that you use REPL or .NET Interactive to learn C#, which is more effective.

▌REPL

What is REPL

REPL (pronounced "REP-UL") is an interactive way for a programming language to talk to a computer. Originally used in Python, many programming languages now support REPL. In fact the REPL does four steps:

  1. Read - Reads the user's input code (such as an entered C# statement).
  2. Evaluate - Evaluate input code (check syntax/understand code meaning).
  3. Print - prints the execution result.
  4. Loop - Continue the interaction, looping 1, 2, 3 steps of work.

In the early days, the best REPL I used was Xamarin's Workbooks. It can not only run C# code, but also write some effects such as Xamarin for iOS, Xamarin for Android, Xamarin Forms, etc. It can also introduce some third-party libraries to achieve some effects. This is very helpful for initial development and prototyping of cross-platform mobile applications.

To use the REPL in .NET, you can use a third-party REPL tool and execute the following command in the terminal

 dotnet tool install --global dotnet-repl

After the installation is successful, enter the following command in the terminal to enter the REPL environment

 dotnet repl

After entering, you can enter any code block to complete the operation

Of course, you can also add relevant third-party libraries/packages through nuget in REPL for testing, such as OpenCVSharp

If you don't like to use the command line method, introduce an official tool .NET Interactive, which is a very useful REPL interactive visualization tool under the .NET system. You can do this by installing the .NET Interactive Notebooks plugin in Visual Studio Code. (If you want to know more detailed installation methods and usage, you can read this article I wrote earlier. NET Interactive environment introduction )

Lambda and LINQ are the syntactic beauty of C#

Each language has its own advantages and disadvantages, and its own application scenarios, traditional type representation, conditional/loop statements, and type descriptions, each language has it, but I think C# is the most elegant The parties are Lambda expressions and LINQ. This is also the content that I highly recommend for entry partners to read intensively.

▌Lambda expressions

A lambda expression is a small piece of code that takes arguments and returns a value. Lambda expressions are similar to methods, but they do not require a name and can be implemented directly in the method body. As of version 2.0, C# already supports Lambda expressions, which is earlier than many programming languages.

Lambda expressions in C# are used in the same way as anonymous functions, except that in lambda expressions you do not need to specify the type of the input value, so it is more flexible to use. '=>ʼ is the lambda operator used for all lambda expressions. A lambda expression is divided into two parts, the input on the left and the expression on the right.

  • An expression lambda with the expression as its body:
 (input-parameters) => expression

Such as:

  • A statement lambda with a statement block as its body:
 (input-parameters) => { <sequence-of-statements> }

Such as:

If you plan to use LINQ in your code, then Lambda expressions will be your favorite companion. Helps you wrap your code logic in fewer lines or inline.

▌LINQ language⾔

LINQ is a collection of technologies that integrate query functionality directly into the C# language. LINQ was also born in C# 2.0 and is used in SQL databases, XML documents, and various Web services. With LINQ, queries are the highest-level language constructs, just like classes, methods, and events.

Such as:

I personally prefer to use the LINQ to SQL syntax, which saves a lot of time in writing T-SQL statements

 using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<string> query = from p in context.Products
                               where p.Name == "Reflector"
                               select p.Name;
​
    IEnumerable<bool> q = query.Select(c => c.EndsWith("Reflector "));
​
    Console.WriteLine("LINQ to Entities returns: " + q.First());
    Console.WriteLine("CLR returns: " + "Reflector".EndsWith("Reflector "));
}

Learn to use Nuget NuGet

Learn to use Nuget NuGet is a package manager for .NET. NuGet client tools provide the ability to generate and consume packages. The NuGet repository is the central package repository used by all package authors and consumers. You can download many packages of different scenarios through NuGet for scenario application.

Like some small partners, the crawling mentioned in the live broadcast can be done by using Nuget through the third-party package Abot ( for more information, please visit ).

how to write C

Every programming language has its own writing specification, which is also the standard for evaluating good code. There are very good coding rules in the official website, which is recommended for everyone to read, which is very helpful for writing C# code. You can also take a look at the open source projects on GitHub , which will also be of great help.

postscript

The new generation of .NET is attractive and covers multiple use cases. As a very important .NET programming language, C# is also a required course for everyone. I hope you can master some tools, programming skills and related points for learning C# through this article. Of course, I also hope that you will continue to participate in the .NET 20th Anniversary Learning Challenge - ASP.NET Core Development.

From April 6th to April 24th, copy the link https://aka.ms/CSCdotNETCore to the browser, or click to participate , you can have the opportunity to get the .NET 20th anniversary peripheral package, .NET professional books, and .NET Conf China 2022 Tickets!

Related Learning Resources

  1. C# learning
  2. C# Lambda Expressions
  3. C# LINQ expressions
  4. NuGet related knowledge

微软技术栈
423 声望996 粉丝

微软技术生态官方平台。予力众生,成就不凡!微软致力于用技术改变世界,助力企业实现数字化转型。