Other options than setting IEnumerable to optional [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
From what I have read IEnumerable cannot be set to optional. I have a method public IEnumerable<StatusInt> StatusInts { get; set; } that always passes in the default of 0 if the user does not select an option from a combo box. For simple data types I can just add ? and the parameter becomes optional and null is passed in. Is there anything similar that I can do with IEnumerable

Value types can be set to optional with the ? character.
int?' is just short for Nullable<int>
IEnumerable is not a value type, it's a reference type. Reference types are already optional - you can return null if you want.
IEnumerable<StatusInt> StatusInts
{
get
{
if(this.OptionIsSelected)
{
return GenerateOptions(this.Option);
}
else
{
return null;
}
}
}
However, I recommend you don't do this. Instead, return an empty enumerable. This way your calling code does not need to check for a null value. You are less likely to inadvertently trigger a null reference exception.
IEnumerable<StatusInt> StatusInts
{
get
{
if(this.OptionIsSelected)
{
return GenerateOptions(this.Option);
}
else
{
return Enumerable.Empty<StatusInt>();
}
}
}

Related

How to set nullable getter setter and set default value if null C# .net [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have this in my code.
public string? additional_data { get; set; }
but my code is not accepting this -> ?
must be using c# version 8, but we are only required to use target framework 4
for example the expected value is below:
additional_data = null
Anyone can help with the proper code for a nullable getter setter?
Few points:
For initializing as null you should use null instead of "NULL" like this:
public string? additional_data { get; set; } = null;
No need of initializing null in your case, as the default value of string? is already null.
So, this should be sufficient:
public string? additional_data { get; set; }
I am only guessing what you require since your post is not very descriptive. I am assuming what you want is a default string value of "NULL" if the value is null?
private string _additionalData;
public string AdditionalData
{
get { return _addtionalData ?? "NULL"; }
set { _additionalData = value; }
}

Is there a good practice of setting default values in an constructor? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Consider a simple class
public class MyClass
{
private int myProperty
...
public int MyProperty
{
get
{
return myProperty;
}
set
{
// some evaluation/condition
myProperty= value;
}
}
...
}
Now, if I want to create an empty constructor where I set default values for the class properties I could do this either this way:
public MyClass()
{
myProperty = 1;
...
}
or this way:
public MyClass()
{
MyProperty = 1;
...
}
Both examples seem valid, since I would never set a default value, that doesn't meet the requirements in the setter evaluation.
The question is, is there a best practice or doesn't it matter anyway?
What would be the advantage of one or the other be (as I can't find any)? Is there some reference, where this question is adressed?
So far I have come across code from many different developers that use either or both ways...
You can use both. But i prefer the first one. Why? Because the value that the property uses is directly assigned. For C# 6 above, you can use default value in a property directly without using constructor.
public class Person
{
public string FirstName { get; set; } = "<first_name>";
public string LastName { get; set; } = "<last_name">;
}
I personally like to set it as you done in first block.
For me it serve as additional fact of method is constructing object, not using alredy constructed. Also it makes me sure that properties is not called (they transform to set/get functions which results in couple of excess instruction).
But i believe that both variants are valid and maybe compiler optimizes properties to direct assignment.
For simple data first method is ok. But on more complex data, you could have a condition in the set (depending to another variable for example, set { if (Config.TestEnv) ...} so if you directly set the private value, you could be in trouble.

Should a property be converted to one with a backing field when there's logic? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
If I have a class with a property that has built-in logic for example:
class myModelClass
{
public List<SummaryModel> Coupons {get;set;}
public decimal AlaCarteTotal
{
get
{
if (Coupons != null)
{
if (!Coupons.Any(x => x.GromotionApplied))
{
return Coupons.Sum(x => x.DefaultPrice);
}
return Coupons.Sum(x => x.GromotionApplied ? x.GromoPrice : x.DefaultPrice);
}
return default(decimal);
}
}
}
...would it be better to convert Coupons to have a backing field such as here:
class myModelClass
{
public List<SummaryModel> Coupons
{
get
{
return _Coupons;
}
set
{
_Coupons = value;
}
}
public decimal AlaCarteTotal
{
get
{
if (_Coupons != null)
{
if (!_Coupons.Any(x => x.GromotionApplied))
{
return _Coupons.Sum(x => x.DefaultPrice);
}
return _Coupons.Sum(x => x.GromotionApplied ? x.GromoPrice : x.DefaultPrice);
}
return default(decimal);
}
}
}
More generally, when a model class contains some kind of getter logic, should the property be converted to one with a backing field?
There is no general recommendation.
It sometime happens that you use some local variables in code and by time you realize you need more complex workflow for setting and obtaining that value.
In case you already have your field as property with (even generic) setter and getter you have your life easier because you just edit your get and set without touching rest of code.
In short: Keep it without backing field until you will have purpose for it. .NET creates it in background anyway but in case you don't need them for your logic your code will be shorter, more brief.
This one could explain it even more: Properties backing field - What is it good for?

Class level variable? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I get this error object reference is required on clindID in the submethod
Why can't I access the string clientID in the sub class Methods? I'd like to use it in multiple methods.
class Remote
{
public string clientID
{
set{} get { return this.clientID; }
}
public bool validClientId()
{
clientID="32";
return true;
}
// closing bracket?
Or would it be better to use
string clientID="";
which doesn't work either
You don't have a setter implemented.
public string clientID
{
get { return this.patientID; }
set { this.patientID = value; }
}
Because you didn't reference the variable using an object reference in your validClientId() method (as described by T McKeown's answer), your code is looking within the scope of the validClientId() method itself to find that variable. It can't find it because the variable hasn't been declared within that scope. Try including the object reference as described by T McKeown to force the compiler to look within the this object for that variable.
Also, your class brackets aren't closed. This might just be a problem with your example code, but you need a closing curly brace }

How can I to check a property will be renewed or its value will be changed [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I to check a property will be renewed or its value will be changed.
public MyClass A
{
get{}
set
{
// find if programmer set A=new MyClass();
}
}
It's not all clear what you want exactly, maybe something like this:
MyClass _a;
public MyClass A
{
get { return _a; }
set
{
// find if programmer set A=new MyClass();
if (value != _a)
{
// different value
}
}
}
For more complex scenario follow the suggestion of Raphaƫl Althaus and take a look at INotifyPropertyChanged.

Categories

Resources