Swift vs C# – Similar semantics, but Swift is more wrist friendly
31 lines in C#:
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias; Rectangle rect = this.ClientRectangle; int cx = rect.Width; int cy = rect.Height; float scale = (float)cy / (float)cx; using (LinearGradientBrush brush = new LinearGradientBrush( this.ClientRectangle, Color.Empty, Color.Empty, 45)) { ColorBlend blend = new ColorBlend(); blend.Colors = new Color[] { Color.Red, Color.Green, Color.Blue }; blend.Positions = new float[] { 0, 0.5, 1 }; brush.InterpolationColors = blend; using (Pen pen = new Pen(brush)) { for (int x = 0; x < cx; x += 7) { g.DrawLine(pen, 0, x * scale, cx - x, 0); g.DrawLine(pen, 0, (cx - x) * scale, cx - x, cx * scale); g.DrawLine(pen, cx - x, 0 * scale, cx, (cx - x) * scale); g.DrawLine(pen, cx - x, cx * scale, cx, x * scale); } } using (StringFormat format = new StringFormat()) { format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; string s = "Ain't graphics cool?"; g.DrawString(s, this.Font, brush, rect, format); } } }
Same number of lines in Swift version (with extra lines added for clarity) compiled via Elements Compiler but type inferencing means shorter declarations.
private func MainForm_Paint(_ sender: System.Object!, _ e: System.Windows.Forms.PaintEventArgs!) { var g = e.Graphics g.SmoothingMode = SmoothingMode.AntiAlias var rect = self.ClientRectangle var cx = Float(rect.Width), cy = Float(rect.Height) var scale = cy/cx var brush = LinearGradientBrush(self.ClientRectangle, Color.Empty, Color.Empty, 45); defer { brush.Dispose() } var blend = ColorBlend() blend.Colors = [Color.Red, Color.Green, Color.Blue] blend.Positions = [ 0, 0.5, 1 ] brush.InterpolationColors=blend var pen = Pen(brush); defer { pen.Dispose() } for x in stride(from: 0, to:rect.Width, by:7 ){ g.DrawLine(pen, 0.0, x*scale, cx-x, 0.0) g.DrawLine(pen, 0.0, (cx-x)*scale, cx-x, cx*scale) g.DrawLine(pen, cx-x, 0*scale, cx, (cx-x)*scale) g.DrawLine(pen, cx-x, cx*scale, cx, x*scale) } var format = StringFormat(); defer { format.Dispose() } format.Alignment = StringAlignment.Center format.LineAlignment = StringAlignment.Center var s = "Ain't graphics cool?" g.DrawString(s,self.Font,brush,rect,format) }