3

My code with target framework .net 2.0 complies and initializes the auto implemented property in following code where as we can initialize auto implemented properties from C#6 which came in .net version 4.6.

    class Program
    {
        static void Main()
        {
            Circle cr = new Circle();

            Console.WriteLine("Radius=" + cr.Radius);

        }
    }

    class Circle
    {
        public double Radius
        {
            get;
            set;
        } = 12.45;  // Initializing Auto Implemented property
    }
  • 2
    Language version and runtime version are not linked entities. Yes, C#6.0 came out with .NET version x.x, but the C#6.0 compiler can still target .NET 2. – spender Apr 09 '18 at 10:23
  • "auto implemented properties from C#6 which came in .net version 4.6." That´s whrong. While C#6 came wih VS2015 (?) it is often compiled for .NET 4.6. However you can surely compile against *every* .NET-framework-version - even against .NET 2.0. Have a look at [difference between C# and .NET](https://stackoverflow.com/questions/2724864/what-is-the-difference-between-c-sharp-and-net). In fact C#-version (which is the **language**) and .NET (which is a framework **upon** a language) don´t rely on each other. – MakePeaceGreatAgain Apr 09 '18 at 10:35

1 Answers1

5

Auto-implemented properties have been introduced with C# 3. C# 3 works with the .NET Framework version 2. In C# 6 you can assign a value while declaring auto-implemented properties. This is a language feature, not a CLR feature.

So all of the above is fine: the language does support .NET 2, and the language supports the language feature. There is no reason it shouldn't work.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Thanks for the comment. MSDN says following : In C# 6 and later, you can initialize auto-implemented properties similarly to fields: C# Copy public string FirstName { get; set; } = "Jane"; – Varun Laxman Bhandarkar Apr 09 '18 at 10:20
  • I don't get the question. I did explain that right? – Patrick Hofman Apr 09 '18 at 10:21
  • 4
    @VarunLaxmanBhandarkar Don't confuse the version of .Net with the version of the C# language – Matthew Watson Apr 09 '18 at 10:22
  • Are the features of c#6 available in .net 2.0 ? – Varun Laxman Bhandarkar Apr 09 '18 at 10:22
  • 4
    @VarunLaxmanBhandarkar c# 6 is the **compiler** and mostly depends on the IDE you're using. There are some (a few) language features that require *library features* that might not exist natively in .NET 2.0 - async, value-tuples, LINQ, etc; but in general most language features are fine as they don't depend on such things. Auto-properties, for example, don't depend on anything. And even if there is a dependency: you can often find libraries that add *implementations* of those features for earlier runtimes - LINQBridge, for example – Marc Gravell Apr 09 '18 at 10:24