C# property definition - c#

For C# properties, I can do this:
public class Employee{
public string Name { get; private set; }
public Employee(string name){
Name = name;
}
}
which means that the Name property can be set within the class Employee & can be read publicly.
But, if I want to restrict the set to only within the constructors of the Employee class, I need to do:
public class Employee{
public readonly string Name = String.Empty;
public Employee(string name){
Name = name;
}
}
But, for this case, I had to change the property to a field.
Is there any reason this is not possible/allowed in C#:
public class Employee{
public string Name { get; private readonly set; }
public Employee(string name){
Name = name;
}
}
IMO this will allow us to have properties which can be set only in the constructor & does not require us to change properties to fields...
Thanks!

Use
private readonly string name = Empty.String;
public string Name { get { return name; } }

What's wrong with:
public class Employee
{
private string nameField;
public string Name
{
get
{
return this.nameField;
}
}
public Employee(string name)
{
this.nameField = name;
}

readonly applies to variables and not to methods. set is converted into a method by the compiler, and therefore the readonly attribute makes no sense.
In order to accomplish what you want you need.
public class Employee
{
private readonly string _name;
public string Name
{
get
{
return _name;
}
}
public Employee(string name)
{
_name = name;
}
}

If you're concerned with only setting the properties within the constructor of the class that you're currently within, just make it a property with a private setter and don't set it in the class.. it's not like you don't have control over that situation.
$0.02

You can have
public class Employee{
public string Name { get; private set; }
public Employee(string name){
Name = name;
}
}
Which will make a readonly public property. If you think about it, having a private readonly setter on a public property doesn't really make sense because you're wanting the setter to be readonly which is a method, not a variable.
If you were to make the setter readonly, essentially what you're doing is denying any access whatsoever to setting the value of the property. This is why you need the backing field.

Related

How to automatically create a default constructor which sets default values

Is there any way to auto generate a constructor which looks like this:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public User(int id, string name)
{
Id = 0;
Name = "";
}
}
Currently I am creating a constructor like that with the refactoring tool (CTRL + .):
public User(int id, string name)
{
Id = id;
Name = name;
}
and editing each line afterwards which is pretty cumbersome when you have 20 properties per class. Is there a better way to that?
(Maybe to define a code snippet, so that we can somehow read the class properties with reflection and define them as snippet parameters?)
If you have a class with 20 properties, why do you need a constructor with 20 parameters? Maybe have a sense, but I usually create constructors to initialize properties that are relevant, to simplify the code, not to set all properties.
For your class, you can set the default values when you define the property and all constructors will use this values as the default.
public class User
{
public int Id { get; set; } = 0;
public string Name { get; set; } = string.Empty;
// Here you can even omit the constructor
public User()
{
}
}
Another thing that maybe useful is define a constructor with X parameters and reuse this constructor in other constructors with less parameters:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public User()
: this(0, string.Empty)
{
}
public User(int id, string name)
{
Id = id;
Name = name;
}
}
You can replace this(0, string.Empty) for this(default, default) if you want use the default value of each type.
If you need object create with default value for properties. You can code like this:
public class User
{
public int Id { get; set; } = 0;
public string Name { get; set; } = "";
}
Purpose of quick action "generate constructor" make method contructor for assign value to fields or properties. Don't use it in the case of just assigning default values.
do you mean initialize properties? Initializing properties through the code reflection mechanism also requires one-by-one assignments. For private object properties, it is necessary to de-private encapsulation. The operation of initializing properties in c# is generally to initialize object properties or object initializers in the form of constructors. Thank you hope it helps you
class Program
{
static void Main(string[] args)
{
Student student = new Student()
{
age = 25,
name = "java",
sex = "female"
};
}
class Student
{
public int age { get; set; }
public string name { get; set; }
public string sex { get; set; }
public Student()
{
}
public Student(int age, string name,string sex)
{
this.age = age;
this.name = name;
this.sex = sex;
}
}
}

how would i set the value for this variable?

I have been given this interface to start with. There are a number of functions I must implement.
using System;
using System.Windows.Forms;
public interface IInfoCard
{
string Name { get; set; }
string Category { get; }
string GetDataAsString();
void DisplayData(Panel displayPanel);
void CloseDisplay();
bool EditData();
}
How would I implement the following function into the interface.
class Class2 : IInfoCard
{
public string Name
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
You should use then Abstract classes, not interfaces, then.
public abstract class IInfoCard
{
string Name { get; set; }
string Category { get; }
string GetDataAsString() { return null; }
void DisplayData(Panel displayPanel) {}
void CloseDisplay() {}
bool EditData() { return true;}
}
You are trying to implement a property. You can basically encapsulate a field.
An easy way to do that is using auto-property:
public string Name { get; set; }
Try this
public class Class2 : IInfoCard
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
}
You can use Auto-Implemented Properties
public class Class2 : IInfoCard
{
public string Name { get; set;}
}
See More
You have two options.
Option 1:
Make them Auto properties, in which case compiler creates a private, anonymous backing field.
public string Name {get;set};
Option 2
Defining an explicit backing field, a private field.
private string name;
public string Name
{
get { return name; }
set { name = value; }
}

How to create a subclass with restrictions?

Can we create a subclass of a base class, like creating a view of a table in SQL, in C#?
Example of desired behaviour:
public class EmployeeSpecificUsage : Employee
{
public string firstName;
public string field1;
public int age;
public string Name; //Error! Not implemented in main class
}
public abstract class Employee
{
public string firstName;
public string lastname;
public int age;
public string workTitle;
public string field1;
public string field2;
public string field3;
}
Objectives:
Reduce the number of field members visible.
Forbid adding new fields, respecting the original model.
Your understanding of subclassing is incorrect. Subclassing is a way of extending the base class, not taking away from it. Whatever a base class has, all subclasses would have it as well.
This is different from views in SQL, which can both take away columns and also add computed columns.
Although inheritance does not allow you to reduce the number of visible members, you can do it with composition. Wrap Employee in a RestrictedEmployee, and expose only the members that you want others to see:
public class EmployeeSpecificUsage {
private readonly Employee wrapped;
public EmployeeSpecificUsage(Employee e) {
wrapped = e;
}
public string firstName => wrapped.firstName;
public string field1 => wrapped.field1;
// Two fields above use C# 6 syntax. If it is not available,
// use syntax below:
public int age {
get {
return wrapped.age;
}
}
}
As far as prohibiting the addition of new fields goes, you cannot do that with either inheritance or composition: if you are allowed to subclass at all (i.e. the base class is not sealed) you would be able to add new members. Composition is a lot weaker than inheritance, so you could add new fields even to sealed classes by wrapping them.
An interface gives you access to an aspect of a class (as opposed to a derived class that actually extends the base class).
Have a look at this:
public class Employee : IEmployeeSpecificUsage
{
public string firstName { get; }
public string lastname { get; }
public int age { get; }
public string workTitle { get; }
public string field1 { get; }
public string field2 { get; }
public string field3 { get; }
}
public interface IEmployeeSpecificUsage
{
public string firstName { get; }
public string field1 { get; }
public int age { get; }
}
If you reference to an Employee instance by the IEmployeeSpecificUsage interface, you will only "see" what's in the interface.
You cannot add new interfaces without changing the "base class", though, as it has to declare that it implements those interfaces.
Interfaces can be used as views.
public interface IView
{
string FirstName { get; }
int Age { get; }
string Name { get; }
}
public class Employee: IView
{
// make fields private if possible
private string firstName;
private string lastname;
private int age;
private string workTitle;
private string field1;
private string field2;
private string field3;
// implements IView.FirstName as an auto property
public string FirstName { get; set; }
// implements IView.Age: returns the private age field
public int Age { get { return age;} }
// explicit implementation of IView.Name: visible only as IView
string IView.Name { get { return lastName + ", " + firstName; } }
}
And then:
Employee employee1 = new Employee(); // FirstName and Age are visible on employee1
IView employee2 = new Employee(); // Name is visible, too
If you want to provide a readonly access to an inner field of a class check out public Properties with getter. Make you field members protected. In such a way you will have an ability to implement custom logic for Name property composing it from firstname and lastname field.
public class EmployeeSpecificUsage : Employee
{
public string FirstName { get { return firstName; }};
public string FullName { get { return string.Format("{0} {1}", firstName, lastName); }};
}
public class Employee
{
protected string firstName;
protected string lastname;
protected int age;
protected string workTitle;
protected string field1;
protected string field2;
protected string field3;
}
An option would be to create properties in the base class with protected setters and public getters.

A property set as private or without private keyword. What is the difference?

I am setting the property of a class like that
public string Name { get; set; }
But i can also set the property like that
public string Name { get; private set; }
I want to know the difference between these? and what scope they have?
It means you cannot set this property from class instance. Only member of same class can set it. Hence for outsiders this property becomes read-only property.
class Foo
{
public string Name1 { get; set; }
public string Name2 { get; private set; }
public string Name3 { get { return Name2; } set { Name2 = value; }
}
Then
Foo f = new Foo();
f.Name1 = ""; // No Error
f.Name2 = ""; // Error.
f.Name3 = ""; // No Error
Name3 will set value in Name2 but setting value in Name2 directly is not possible.
and what scope they have?
Since Name1 and Name3 property are public so they and their get and set methods are available everywhere.
Name3 is also public but its set is private so property and get method will be available everywhere. Set method scope is limited to class only (private access modifier has scope inside entity where it is defined).
For the case public string Name { get; private set; }
Using private set means that the property is ReadOnly from the outside. Its useful when you have a read only property and don't want to explicitly declare the backing variable.
public string Name { get; private set; } it is same as :
private string _Name;
public string Name
{
get { return _Name; }
private set { _Name = value; }
}
The first one will have Set and Get methods available out of your class. The second one will have a Get method available out of your class but the Set method will only be available within your class. This usually denotes read only behaviour.

override get only

Assuming I have:
public abstract class A {
public abstract string Name { get; }
}
I want to use the "shortcut" for creating properties in the child class:
public string Name { get; set; }
Instead of:
private string _Name;
public string Name {
get{ return _Name; }
set{ _Name=Name; }
}
But this cause compilation error since I have set; there
public class B {
public override Name{get; set;}
}
How can I override only get;?
Just implement the getter:
public override string Name
{
get
{
return this.name;
}
}
private string name;
The shorthand for a simple get + set property is great if you want a simple get + set property, but if you want something more complicated (such as a property with just a getter) you need to implement it yourself.
Short answer: there is no shorthand way
public class B {
private string iName;
public override string Name{get{ return iName;}}
}
on a side note:
You could make your base class an interface and then override in the implementing class:
public interface A {
string Name { get; }
}
public class B : A {
public string Name {get;set;}
}

Categories