Run Your First C# Program
Lesson 1 of 48 in Coddy's C# for Swift Developers course.
A Swift executable can use top-level statements in main.swift. This C# runner starts a static Main method on a class and compiles sibling .cs files together. Swift source references run separately; the challenge editor runs Mono C#.
C# is a statically checked language in the .NET family. This course uses a compatible Mono compiler and explicit classes and methods. You do not need modern records, top-level statements or nullable-reference annotations. Start with one successful run; the following lessons explain the syntax and program structure.
Swift reference
import Foundation
print("C# is ready")C# statements inside Main
Console.WriteLine("C# is ready");These are focused teaching excerpts. The challenge files include complete executable wrappers; the snippets above show the relevant declarations and statements.
Reference output:
C# is readyC# output:
C# is readyChallenge
EasyRun the supplied program unchanged. There is no input. It prints C# is ready. Later tasks put your editable code in a separate C# source file from the visible locked launcher.
Edit the unlocked C# files. The visible locked Main.cs reads the described lines, calls Exercise.Solve, verifies input was not mutated and prints the result with one final newline. Keep the declared APIs. Do not add a second Main or print diagnostic output.
Try it yourself
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
public class Program {
static int I(string text) { return int.Parse(text,CultureInfo.InvariantCulture); }
static int[] Ints(string text) { return text.Length==0 ? new int[0] : text.Split(',').Select(I).ToArray(); }
static string[] Words(string text) { return text.Length==0 ? new string[0] : text.Split(','); }
static string Snapshot(object value) { if (value==null) return "<null>"; if (value is string) return (string)value; var sequence=value as System.Collections.IEnumerable; if (sequence!=null) { var items=new List<string>(); foreach (object item in sequence) items.Add(Snapshot(item)); return string.Join("|",items); } return Convert.ToString(value,CultureInfo.InvariantCulture); }
public static void Main() {
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var result = Exercise.Solve();
Console.WriteLine(result);
}
}
All lessons in C# for Swift Developers
1Start Here
Run Your First C# Program4Loops and Sequences
Arrays and BoundsIndexed for Loopsforeach, break and continueLists and Safe UpdatesRecap: Positive Prefix7Reliable C# Modules
Specific Exception HandlingNamespaces and FilesValue and Reference SemanticsDeterministic Resource CleanupRecap: Import Valid Numbers10Project: Order Pipeline
Project: Order ModelProject: Validate Rows2C# Values and Text
Output, Comments and MainTypes, var and AssignmentInteger and Decimal MathStrings, char and FormattingParse Without ExceptionsRecap: Receipt Line5Collections and Queries
Dictionaries and Missing KeysSets and MembershipLINQ Filters and ProjectionsSorting Without MutationRecap: Word InventoryPractice on your own: Online C# compiler