CSharp C# – New Class with inline update to public members
This example shows the syntax to immediately populate class members with bracket and comma syntax:
snippet:
MyClass myc = new MyClass()
{
pInt = 2,
sString = "My inline string"
};
meta: inline in-line parameters instantiate object
program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestingStuff
{
class Program
{
static void Main(string[] args)
{
MyClass myc = new MyClass()
{
pInt = 2,
sString = "My inline string"
};
Console.WriteLine("myc pInt{0} and sString = {1}", myc.pInt, myc.sString);
Console.WriteLine();
Console.ReadLine();
/*
OUTPUT:
myc pInt2 and sString = My inline string
*/
}
public class MyClass
{
public int pInt { get; set; }
public string sString { get; set; }
public MyClass() { }
}
}
}
No comments:
Post a Comment