0

I came across a tutorial on Full Stack .Net Web Development and I've never seen the use of the dot operator is this way for C# as of yet. Can someone explain what it means in regards to why don't the statements end in semicolons and also do the statements belong to a specific object?

using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;

namespace MyApi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}
Jon Bates
  • 3,055
  • 2
  • 30
  • 48
Laurence Wingo
  • 3,912
  • 7
  • 33
  • 61
  • 12
    They've just added whitespace where whitespace is allowed. It's exactly the same as `new WebHostBuilder().UseKestrel().UseContentRoot(Directory.GetCurrentDirectory())....` all written on one line. – Damien_The_Unbeliever Jan 03 '19 at 14:23
  • 5
    One semi-colon is missing after `.Build()` though – Rafalon Jan 03 '19 at 14:24
  • 2
    @Heinzi This is not actually a duplicate. The linked question is about terminology, this one is about valid syntaxes and whitespace, even though both refer to similar pieces of code. – Alejandro Jan 03 '19 at 14:36
  • @Alejandro: Actually, the question doesn't really mention whitespace at all, but I agree that it could be seen as different. The question is: Is it different enough? Is it beneficial to future readers to have this as a separate (non-dupe-linked) question with separate answers? – Heinzi Jan 03 '19 at 15:07
  • 1
    @Heinzi Well, no, the answer is all about whitespace, the question could be improved to something like "Why is this valid syntax?", while the linked is about using chained methods, and this one is about C# syntax, in particular to chained methods, but can also apply to more situations (not stated here). If you look at all the answers in the link, no one uses this exact syntax and they don't explain why it's valid C#, just how methods are called one after another. This one is about source code, the other is about execution and design, which make me think that it's worty to have both of them. – Alejandro Jan 03 '19 at 15:20
  • @Alejandro thanks because its still not clear to me as to why towards the end there is a method called Run which begins with calling the host object however the other methods don't. I'm not 100% sure if the other methods are a part of the object called host. – Laurence Wingo Jan 03 '19 at 15:21
  • 1
    @Alejandro: OK, I have reopened the question. Feel free to write an answer. :-) – Heinzi Jan 03 '19 at 16:01

1 Answers1

5

It is simply a single statement split across multiple lines, and also an example of fluent method chaining.

Each method call returns an object, which then can be de-referenced to perform another method call.

Here's a simple-ish example to give you an idea of how it works. Note how each method returns the current instance of Person, i.e. this.

class Person
{
    public string Firstname { get; set; }
    public string Surname { get; set; }
    public DateTime DateOfBirth { get; set; }
    public decimal HeightCm { get; set; }

    public Person WithName(string firstname, string surname)
    {
        Firstname = firstname;
        Surname = surname;
        return this;
    }

    public Person BornOn(DateTime date)
    {
        DateOfBirth = date;
        return this;
    }

    public Person WithHeight(decimal heightCm)
    {
        HeightCm = heightCm;        
        return this;
    }
}

You can then do the following:

var person = new Person().WithName("Doctor", "Jones").BornOn(new DateTime(1980, 1, 1)).WithHeight(175);

Which can also be expressed as:

var person = new Person()
    .WithName("Doctor", "Jones")
    .BornOn(new DateTime(1980, 1, 1))
    .WithHeight(175);

Splitting it across multiple lines is not necessary, but could either be a stylistic choice, or dictated by your coding standards.

Doctor Jones
  • 21,196
  • 13
  • 77
  • 99