core — examples
← all topics · 500 examples · page 1 of 10 · raw source ↓
Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: EmitterStressTests.cs::NullConditionalChainWithCoalesce topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func safe(s: string) -> int {
return s?.Length ?? 0
}
func ternaryChain(x: int) -> string {
let label = x > 100 ? "high" : x > 50 ? "mid" : "low"
return label
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: EmitterStressTests.cs::TryCatchWithTypedBinding topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func run() -> string {
try {
let x = 1 / 0
return "ok"
} catch (Exception ex) {
return ex.Message
}
}Runs `check` → false
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FeatureMixingTests.cs::Char_LetThenCompare topic: core status: verified
// verified behavior: Test.check(...) == false
namespace Test
func check(s: string, i: int) -> bool {
let target = 'X'
return s[i] == target
}Runs `isNewline` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FeatureMixingTests.cs::Char_LiteralEqualityAgainstStringIndex topic: core status: verified
// verified behavior: Test.isNewline(...) == true
namespace Test
func isSpace(s: string, i: int) -> bool {
return s[i] == ' '
}
func isQuote(s: string, i: int) -> bool {
return s[i] == '"'
}
func isNewline(s: string, i: int) -> bool {
return s[i] == '\n'
}Runs `isLetterAt` → false
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FeatureMixingTests.cs::Char_PassedToBclMethod topic: core status: verified
// verified behavior: Test.isLetterAt(...) == false
namespace Test
func isLetterAt(s: string, i: int) -> bool {
return char.IsLetter(s[i])
}
func isDigitAt(s: string, i: int) -> bool {
return char.IsDigit(s[i])
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Attributes_OnFunction_EmittedAsCLRAttributes topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
[Obsolete]
func oldMethod() -> int {
return 0
}Runs `run` → 15
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::ByRef_MutatesThroughPointer topic: core status: verified
// verified behavior: Test.run(...) == 15
namespace Test
func addTen(x: *int) {
x += 10
}
func run() -> int {
var n = 5
addTen(*n)
return n
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Closure_CapturedLet_ReadsCorrectly topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func makeAdder(base: int) -> int {
let offset = 100
let add = func(x: int) -> int { return offset + x }
return add(base)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Closure_CapturedVar_MutationPropagates topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func evalWith(value: int, offset: int) -> int {
var total = 0
let addTo = func(v: int) { total = total + v }
addTo(value)
addTo(offset)
return total
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Closure_NoCaptureIL_EmitsDelegate topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func apply(f: int, g: int) -> int {
let double = func(x: int) -> int { return x * 2 }
return double(f)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::CompoundAssignment_IL_Runtime topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func accumulate(n: int) -> int {
var total = 0
total += n
total += n
total -= 1
return total
}Runs `run` → false
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Default_BoolIsFalse topic: core status: verified
// verified behavior: Test.run(...) == false
namespace Test
func run() -> bool {
let b = default(bool)
return b
}Runs `run` → 0
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Default_IntIsZero topic: core status: verified
// verified behavior: Test.run(...) == 0
namespace Test
func run() -> int {
let x = default(int)
return x
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::DelegateInvoke_ActionVoid_IL_Runtime topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func runCallback(x: int) -> int {
var result = 0
let cb = func(v: int) { result = v * 2 }
cb(x)
return result
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::EntryPoint_Transpiler_EmitsMain topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func main() {
var x = 42
}Runs `run` → 2
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Event_Subscribe_ObservableCollectionCollectionChanged topic: core status: verified
// verified behavior: Test.run(...) == 2
namespace Test
func run() -> int {
var fired = 0
let coll = ObservableCollection<int>()
coll.CollectionChanged += func(sender: object, args: NotifyCollectionChangedEventArgs) -> void {
fired = fired + 1
}
coll.Add(10)
coll.Add(20)
return fired
}Runs `always42` → 42
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::ExpressionBodied_SimpleReturn topic: core status: verified
// verified behavior: Test.always42(...) == 42
namespace Test
func double(x: int) -> int = x * 2
func negate(x: int) -> int = 0 - x
func always42() -> int = 42Runs `run` → 3
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Extension_LinqCount_OnList topic: core status: verified
// verified behavior: Test.run(...) == 3
namespace Test
func run() -> int {
let nums = List<int>()
nums.Add(1)
nums.Add(2)
nums.Add(3)
return nums.Count()
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::ExternalCall_ConsoleWriteLine_IL topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func main() {
Console.WriteLine("hello")
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::FunctionPointer_Ldftn_EmitsCorrectOpcode topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func double(x: int) -> int {
return x * 2
}
func getDoublePtr() -> int {
let ptr = &double
return 0
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::FunctionPointer_TranspilerEmitsDelegateStar topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func double(x: int) -> int {
return x * 2
}
func getPtr() -> int {
let ptr = &double
return 0
}Runs `run` → 2
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::GenericCall_EnumerableCountOnList topic: core status: verified
// verified behavior: Test.run(...) == 2
namespace Test
func run() -> int {
let nums = List<int>()
nums.Add(10)
nums.Add(20)
return Enumerable.Count<int>(nums)
}Runs `run` → 6
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::GenericCall_InferenceStillWorks topic: core status: verified
// verified behavior: Test.run(...) == 6
namespace Test
func run() -> int {
let nums = List<int>()
nums.Add(1)
nums.Add(2)
nums.Add(3)
return nums.Sum()
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::GenericConstructorCall_List_ParsesCorrectly topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func make() -> int {
let items = List<string>()
return 0
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::GenericExternalType_Runtime_ListAdd topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func run() -> int {
var count = 0
count = 3
return count
}Runs `run` → 0
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::GenericTypeWithTupleArg_NoStackOverflow topic: core status: verified
// verified behavior: Test.run(...) == 0
namespace Test
func run() -> int {
var xs = List<(int, int)>()
return xs.Count
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::IL_Diagnostics_UnresolvedType_ReportsError topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func broken(x: Nonexistent) -> int {
return 0
}Runs `run` → 99
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Indexer_ListGetSet_RoundTrip topic: core status: verified
// verified behavior: Test.run(...) == 99
namespace Test
func run() -> int {
let list = List<int>()
list.Add(10)
list.Add(20)
list.Add(30)
list[1] = 99
return list[1]
}Runs `runFound` → "found"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::LetElse_ReturnsOnNull topic: core status: verified
// verified behavior: Test.runFound(...) == "found"
namespace Test
func tryFind(key: string) -> string? {
if key == "a" {
return "found"
}
return nil
}
func run() -> string {
let value = tryFind("b") else {
return "default"
}
return value
}
func runFound() -> string {
let value = tryFind("a") else {
return "default"
}
return value
}Runs `first` → 42
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::ListLiteral_IntElements topic: core status: verified
// verified behavior: Test.first(...) == 42
namespace Test
func count() -> int {
let xs = [10, 20, 30]
return xs.Count
}
func first() -> int {
let xs = [42, 99]
return xs[0]
}Runs `joined` → "hello"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::ListLiteral_StringElements topic: core status: verified
// verified behavior: Test.joined(...) == "hello"
namespace Test
func joined() -> string {
let xs = ["hello", "world"]
return xs[0]
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::MultiFile_DistinctNamespaces_CoexistInOneAssembly topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Alpha
func a() -> int { return 1 }Runs `run` → 0
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::NestedGenericWithTupleArg_NoStackOverflow topic: core status: verified
// verified behavior: Test.run(...) == 0
namespace Test
func run() -> int {
var xs = List<(List<int>, List<int>)>()
return xs.Count
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::NoAwait_Binder_HasAwaitIsFalse topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func add(a: int, b: int) -> int {
return a + b
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Nullable_NilReturn_ReferenceType_Runtime topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func tryFind(s: string) -> string? {
return nil
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Nullable_NilReturn_ValueType_Runtime topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func tryParse(s: string) -> int? {
return nil
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Nullable_ValueType_ResolvesToSystemNullable topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func maybe(x: int?) -> int {
return 0
}Runs `fallback` → "hello"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::NullCoalescing_StringFallback topic: core status: verified
// verified behavior: Test.fallback(...) == "hello"
namespace Test
func fallback(s: string) -> string {
return s ?? "default"
}Runs `safeLen` → 5
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::NullConditional_ChainedWithCoalescing topic: core status: verified
// verified behavior: Test.safeLen(...) == 5
namespace Test
func safeLen(s: string) -> int {
let result = s ?? ""
return result.Length
}
func withCoalesce(s: string) -> string {
return s ?? "none"
}Runs `run` → -1
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Out_IntTryParse_Failure topic: core status: verified
// verified behavior: Test.run(...) == -1
namespace Test
func run() -> int {
if int.TryParse("not a number", out var n) {
return n
}
return -1
}Runs `run` → 42
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Out_IntTryParse_Success topic: core status: verified
// verified behavior: Test.run(...) == 42
namespace Test
func run() -> int {
if int.TryParse("42", out var n) {
return n
}
return -1
}Runs `run` → "hello world"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Params_StringFormat_NoExtraArgs topic: core status: verified
// verified behavior: Test.run(...) == "hello world"
namespace Test
func run() -> string {
return string.Format("hello {0}", "world")
}Runs `run` → "1 + 2 = 3"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Params_StringFormat_TwoArgs topic: core status: verified
// verified behavior: Test.run(...) == "1 + 2 = 3"
namespace Test
func run() -> string {
return string.Format("{0} + {1} = {2}", 1, 2, 3)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Parse_ForTupleDestructuring topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func run() {
let results = List<(int, int)>()
for (a, b) in results {
let x = a + b
}
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Parse_ImportStaticAndArrowLambda topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace T
using static "System.Math"
func run() -> double {
return Max(1.0, ((x) => x + 2.0)(3.0))
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Parse_NullConditionalAndCoalesceInTernary topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func run() {
let s: string = nil
let x = s?.Length ?? 0
let y = x > 0 ? s : "default"
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Parse_TryCatchWithTypedBinding topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func run() {
try {
let x = 1
} catch (Exception ex) {
let msg = ex.Message
}
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Parse_TupleTypeInGeneric topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func test() -> int {
var tasks = List<Task<(int, string)>>()
return tasks.Count
}Runs `run` → 3
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Property_GetFromBclObject_ListCount topic: core status: verified
// verified behavior: Test.run(...) == 3
namespace Test
func run() -> int {
let list = List<int>()
list.Add(1)
list.Add(2)
list.Add(3)
return list.Count
}Runs `run` → 5
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Property_GetFromBclObject_StringLength topic: core status: verified
// verified behavior: Test.run(...) == 5
namespace Test
func run() -> int {
let s = "hello"
return s.Length
}