Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0111 Constructors with named parameters

We can use the named parameters with constructors to initialize the fields. using System; class Rectangle { public int Width; public int Height; public Rectangle(int w, int h){ Width = w; Height = h; } } class Program { static void Main(string[] args) { Rectangle r = new Rectangle(h : 6, w:5); Console.WriteLine(r.Width); Console.WriteLine(r.Height); } } The output: 5 6