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;
}
}
}
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With