Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a class in C# 6.0 have a protected primary constructor?

Tags:

This class:

class Person 
{
    public Person(string firstName, string lastName)
    {
        _firstName = FirstName;
        _lastName = lastName;
    }

    private readonly string _firstName; // Make it really immutable
    public string FirstName
    {
        get
        {
            return _firstName;
        }
    }

    private readonly string _lastName; // Make it really immutable
    public string LastName
    {
        get
        {
            return _lastName;
        }
    }
}

Can be rewritten in C# version 6.0 with a primary constructor as:

class Person(string firstName, string lastName)
{
    public string FirstName { get; } = firstName;
    public string LastName { get; } = lastName;
}

Is it possible to give the primary constructor a different modifier like in the following class?

abstract class Person 
{
    protected Person(string firstName, string lastName)
    {
        _firstName = FirstName;
        _lastName = lastName;
    }

    private readonly string _firstName; // Make it really immutable
    public string FirstName
    {
        get
        {
            return _firstName;
        }
    }

    private readonly string _lastName; // Make it really immutable
    public string LastName
    {
        get
        {
            return _lastName;
        }
    }
}
like image 996
Alex Siepman Avatar asked Aug 22 '14 19:08

Alex Siepman


People also ask

What is a class in C language?

A class is an extended concept similar to that of structure in C programming language; this class describes the data properties alone. In C++ programming language, a class describes both the properties (data) and behaviors (functions) of objects. Classes are not objects, but they are used to instantiate objects.

Can main be inside a class?

There can only be one main function in a class and it must always be static, meaning it is not part of an object and there is only one instance of it. When a java application is executed, the JRE will look for the main class (i.e. the class containing the main function). main() is where the execution starts.

Can main be in a class C++?

Writing a class named main is not allowed generally in C++, as the compiler gets confused it with main() method. Hence when we write the main class, creating its object will lead to error as it won't consider the 'main' as a class name.

Is C++ C with classes?

Originally, C++ was called “C with classes,” as it had all the properties of the C language with the addition of user-defined data types called “classes.” It was renamed C++ in 1983. C++ is considered an intermediate-level language, as it includes both high and low-level language features.


1 Answers

EDIT: This answer talks about pre-release version of C# 6.0. The final release version of C# 6.0 doesn't have primary constructors at all.


The Language Design Notes for 21 April say (and I didn't find any later reference to this):

Separate accessibility on type and primary constructor

There are scenarios where you don’t want the constructors of your type to have the same accessibility as the type. A common case is where the type is public, but the constructor is private or protected, object construction being exposed only through factories.

Should we invent syntax so that a primary constructor can get a different accessibility than its type?

Conclusion

No. There is no elegant way to address this. This is a fine example of a scenario where developers should just fall back to normal constructor syntax. With the previous decisions above, we’ve done our best to make sure that that cliff isn’t too steep.

So, no, there is no way to declare the primary constructor as protected. Though, as was already pointed out, there is no difference between public and protected constructor for an abstract class.

like image 182
svick Avatar answered Sep 18 '22 03:09

svick