Skip to content

Source only package that exposes newer APIs, .net features, and C# features to older runtimes.

License

Notifications You must be signed in to change notification settings

emako/Polyfill.Example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

简体中文

Polyfill.Example

This repository demonstrates how to provide polyfill support for C# 9.0+ features (such as record, init-only setter, Index/Range, etc.) in legacy .NET environments like .NET Framework 4.8.

Directory Structure

Polyfill.Example.slnx
src/
  Polyfill.Example.IndexRange/    // Index/Range feature demo
    Program.cs
    ...
  Polyfill.Example.IsExternalInit/ // isExternalInit feature demo
    Program.cs
    ...

Background

C# 9.0 introduces features like record types and init accessors, but these require .NET 5+ runtime support. In older versions such as .NET Framework 4.8, the lack of System.Runtime.CompilerServices.IsExternalInit causes compilation errors. This project demonstrates how to add custom types and code to enable compatibility with these new features.

Main Content

  • Polyfill.Example.IsExternalInit: Shows how to provide the IsExternalInit type for .NET Framework to support init setters and record.
  • Polyfill.Example.IndexRange: Demonstrates using C# 8.0 Index/Range syntax in legacy .NET environments.

Getting Started

1. Clone the repository

git clone https://github.com/yourname/Polyfill.Example.IsExternalInit.git
cd Polyfill.Example.IsExternalInit

2. Build and Run

For Polyfill.Example.IsExternalInit:

cd src/Polyfill.Example.IsExternalInit
msbuild /p:Configuration=Debug
bin\Debug\net48\Polyfill.Example.IsExternalInit.exe

For Polyfill.Example.IndexRange:

cd src/Polyfill.Example.IndexRange
msbuild /p:Configuration=Debug
bin\Debug\net48\Polyfill.Example.IndexRange.exe

DEMO

1. isExternalInit Polyfill Example

src/Polyfill.Example.IsExternalInit/Program.cs:

// Polyfill for IsExternalInit (only needed for legacy .NET)
namespace System.Runtime.CompilerServices
{
    public class IsExternalInit { }
}

public record Person(string Name) { public int Age { get; init; } }

class Program
{
    static void Main()
    {
        var p = new Person("Zhang San") { Age = 18 };
        Console.WriteLine($"{p.Name}, {p.Age}");
    }
}

Output:

Zhang San, 18

2. Index/Range Polyfill Example

src/Polyfill.Example.IndexRange/Program.cs:

class Program
{
    static void Main()
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        // Use Index/Range syntax
        int last = arr[^1]; // 5
        int[] sub = arr[1..^1]; // {2,3,4}
        Console.WriteLine(last);
        Console.WriteLine(string.Join(",", sub));
    }
}

Output:

5
2,3,4

Reference

License

MIT

About

Source only package that exposes newer APIs, .net features, and C# features to older runtimes.

Topics

Resources

License

Stars

Watchers

Forks

Languages