Is it possible to set version for each class (.cs) file in C#

PERUMAKARASU Mohanapriya-EXT 20 Reputation points
2023-10-31T14:14:34.65+00:00

Is there any possibility to set the versioning for cs files in a single project, if yes please share the suggestion how to do.

Scenario:

If MyFirstProject(.ccsproj) has 3 .cs files

  1. Addition.cs
  2. Subtration.cs
  3. Multiplication.cs

In one release i am updating addition.cs alone and in another release i am updating Subtraction.cs - how do we differentiate?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,922 questions
{count} votes

2 answers

Sort by: Most helpful
  1. 2024-01-06T18:24:00.54+00:00

    A project, by itself using .Net features, cannot hold multiple "versions" of a class in the sense that you cannot decide, at runtime, which version you want:

    public static void Main(string[] args)
    {
        var myAdditionObject = myCalculatorAssembly.Get<Addition>("version1");
    }
    

    There is no such a thing in .Net. If you want to properly version releases of your assembly learn semantic versioning and properly version your releases.

    Now, having said that, you could provide your own mechanism by consumig through an interface:

    // In your library:
    public interface IAddition
    {
        double Add(double x, double y);
    }
    
    public class AdditionV1 : IAddition
    {
        public double Add(double x, double y)
            => x + y;
    }
    
    public class AdditionV2 : IAddition
    {
        public double Add(double x, double y)
        {
            // Your V2 implementation.
            ...
        }
    }
    
    // Then, when consuming from an external project that referenes your DLL:
    
    public static void Main(string[] args)
    {
        IAddition addition = new AdditionV2(); // Or AdditionV1 or any other "version".
    }
    

    This way you instantiate the version you want.

    0 comments No comments

  2. Bruce (SqlWork.com) 65,211 Reputation points
    2024-01-07T21:49:30.7133333+00:00

    while you can version c# file with conditional directives:

    https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives

    as you have git, you should use git. you should make a branch for each release. this will allow you make a patch to release without effecting mainline.

    https://gist.github.com/stuartsaunders/448036/e4978fecf3beacd72ab05a4a45228271a9ce7f00#

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.