The paper presents an approach to implementing traits functionality in the C# programming language through the use of Roslyn Source Generators, a compile-time metaprogramming tool introduced in .NET5. Traits, which are composable units of fine-grained reuse, can benefit the C# language by bringing a mechanism for behavior reuse in the context of single inheritance. The article describes the design and implementation of a source generator that allows to annotate classes with metadata attributes, automatically injecting the corresponding member definitions at compile time. To achieve better code generation performance at design time, Incremental Source Generators were used. The composition of trait members is described in detail, focusing on name conflict resolution and type safety by utilizing interfaces and the override mechanism. Defining non-public members in a context of a trait is also discussed and implemented, thus allowing for better code reusability. The article concludes that language-level features such as traits can be implemented in the C# language by using Roslyn Source Generators, thus demonstrating the potential of such an instrument and metaprogramming in general.
References
[1] Albalooshi F., Mahmood A.: A Comparative Study on the Effect of Multiple Inheritance Mechanism in Java, C++, and Python on Complexity and Reusability of Code. Int. J. Adv. Comput. Sci. Appl. 8(6), 2017, 109–116 [https://doi.org/10.14569/ijacsa.2017.080614].
[2] Arora H., Servetto M., Oliveira B. C. D. S.: Separating Use and Reuse to Improve Both. The Art, Science, and Engineering of Programming 3(3), 2019 [https://doi.org/10.22152/programming-journal.org/2019/3/12].
[3] AutoInterface, 2025 [https://github.com/beakona/AutoInterface].
[4] Bergel A. et al.: Stateful Traits. De Meuter W. (eds): Advances in Smalltalk. ISC 2006. Lecture Notes in Computer Science 4406. Springer, Berlin, Heidelberg 2007 [https://doi.org/10.1007/978-3-540-71836-9_4]
[5] Black A. P., Schärli N., Ducasse S.: Applying Traits to the Smalltalk Collection Classes. ACM SIGPLAN Not. 38(11), 2003, 47–64 [https://doi.org/10.1145/949343.949311].
[6] Cassou D., Ducasse S., Wuyts R.: Traits at work: The design of a new trait-based stream library. Comput. Lang., Syst. & Struct. 35(1), 2009, 2–20 [https://doi.org/10.1016/j.cl.2008.05.004].
[7] Ducasse S. et al.: Traits: A mechanism for fine-grained reuse. ACM Trans. Program. Lang. Syst. 28(2), 2006, 331–388 [https://doi.org/10.1145/1119479.1119483]
[8] Ducournau R., Morandat F., Privat J.: Empirical assessment of object-oriented implementations with multiple inheritance and static typing. ACM SIGPLAN Not. 44(10), 2009, 41–60 [https://doi.org/10.1145/1639949.1640093].
[9] Explicit Interface Implementation – C#, 2022 [https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/explicit-interface-implementation].
[10] Gerr P.: Incremental Roslyn Source Generators In .NET 6: Code Sharing Of The Future – Part 1. 2022 [https://www.thinktecture.com/net/roslyn-source-generators-introduction/].
[11] Hossain M.: Understanding Traits in PHP: A Guide with Examples, 2024 [https://mohasin-dev.medium.com/understanding-traits-in-php-a-guide-with-examples-84f14f1b7b43].
[12] Immutype, 2025 [https://github.com/DevTeam/Immutype].
[13] Introducing C# Source Generators, 2025 [https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/].
[14] Lock A.: Understanding C# 8 default interface methods. 2024 [https://andrewlock.net/understanding-default-interface-methods/].
[15] Mr Prime: Mixins in Ruby, 2024 [https://medium.com/@primedruk312/mixins-in-ruby-09e58b226e50].
[16] Muniyandi V.: Harnessing Roslyn for Advanced Code Analysis and Optimization in Cloud- Based .NET Applications on Microsoft Azure. Int. J. Commun. Netw. Secur. 14(3), 2022, 979–990 [https://ijcnis.org/index.php/ijcnis/article/view/8051/2231].
[17] Nichols C., Klabnik S.: Rust Programming Language, 2nd Edition. No Starch Press, Incorporated, 2023.
[18] Partial Classes and Members – C#, 2025 [https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods].
[19] Schärli N. et al.: Traits: Composable Units of Behaviour. In: ECOOP 2003 – Object-Oriented Programming. Springer Berlin Heidelberg, 2003, 248–274 [https://doi.org/10.1007/978-3-540-45070-2_12].
[20] Steyaert P. et al.: Nested Mixin-Methods in Agora. Nierstrasz O. M. (eds): ECOOP’ 93 — Object-Oriented Programming. Springer Berlin Heidelberg, 1993 [https://doi.org/10.1007/3-540-47910-4_12].
[21] StrongInject, 2025 [https://github.com/YairHalberstadt/stronginject].
[22] Tesone P. et al.: A new modular implementation for stateful traits. Sci. Comput. Program. 195, 2020, 102470 [https://doi.org/10.1016/j.scico.2020.102470].
[23] Traits: Defining Shared Behavior, 2025 [https://doc.rust-lang.org/book/ch10-02-traits.html].
[24] Wampler D., Payne A.: Programming Scala. O'Reilly Media, Incorporated, 2014.
[25] Yaqoob E.: Mixins in dart, 2023 [https://medium.com/@emanyaqoob/mixins-in-dart-aed7e89de15d].