title: C# Setup sidebar_position: 7 id: csharp license: | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Fory C# provides xlang Object Serialization, generated models, and Fory gRPC. The Apache.Fory NuGet package requires .NET 8 or later and includes both the runtime and source generator.
dotnet --version
Create a console project and add the released package:
dotnet new console -n ForyExample cd ForyExample dotnet add package Apache.Fory --version 1.5.0
Replace Program.cs with:
using Apache.Fory; [ForyStruct] public sealed class User { public long Id { get; set; } public string Name { get; set; } = string.Empty; } public static class Program { public static void Main() { Fory fory = Fory.Builder().Build(); fory.Register<User>(1); byte[] bytes = fory.Serialize(new User { Id = 1, Name = "Alice" }); User decoded = fory.Deserialize<User>(bytes); Console.WriteLine(decoded.Name); } }
dotnet run
C# uses xlang mode. Continue with C# Object Serialization, xlang types, configuration, and schema evolution.