C
C#4mo ago
swaysteady

What's this syntax in this object instantiation

This screenshot is straight from MimeKit's quick start guide here: https://mimekit.net/docs/html/Creating-Messages.htm I understand body is being declared as a new object of type TextPart. The constructor is being passed the string "plain", but next to that: that bracketed block, what's the C# structure/syntax being used here? How is that Text variable assignment in the block being associated to the body object? I'd like to look it up but I'm not sure what to go reading about. Can I have some super beginner help? 🙂
No description
4 Replies
Angius
Angius4mo ago
Initializer syntax
MODiX
MODiX4mo ago
Angius
REPL Result: Success
class Foo
{
public int Bar { get; set; }
}

var f = new Foo {
Bar = 69
};

f.Bar
class Foo
{
public int Bar { get; set; }
}

var f = new Foo {
Bar = 69
};

f.Bar
Result: int
69
69
Compile: 281.771ms | Execution: 38.247ms | React with ❌ to remove this embed.
Angius
Angius4mo ago
It basically compiles down to
var f = new Foo();
f.Bar = 69;
var f = new Foo();
f.Bar = 69;
As a side note, if you use a raw string literal (or whatever the triple-quoted string is called) you can have this large string indented better:
message.Body = new TextPart("plain") {
Text = """
Hey Alice,

What are you up to...
"""
};
message.Body = new TextPart("plain") {
Text = """
Hey Alice,

What are you up to...
"""
};
swaysteady
swaysteadyOP4mo ago
That got me right to the microsoft learn page I need to read. Thank you!
Want results from more Discord servers?
Add your server