Null-coalesing or ternary operator prefernce? [closed] - c#

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 2 years ago.
Improve this question
cmd.Parameters.Add("p_Shift_Id", OracleDbType.Varchar2).Value = p_Shift_Id;
p_Shift_Id could be null. If it is null, then I want to use DBNull.Value. If it's not null, then the value that it contains.
What is the best approach? I would rather not use
if(p_Shift_Id == null)
{
cmd.Parameters.Add("p_Shift_Id", OracleDbType.Varchar2).Value = DBNull.Value;
} else {
cmd.Parameters.Add("p_Shift_Id", OracleDbType.Varchar2).Value = p_Shift_Id;
}

Cast p_Shift_Id as object and then use Null-Coalescing operator as follows:
cmd.Parameters.Add("p_Shift_Id", OracleDbType.Varchar2).Value = (object)p_Shift_Id ?? DBNull.Value;

Related

C# Best practice (LINQ) for check a string in a List/Array of strings [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 2 years ago.
Improve this question
Im trying to check if a string in on a list/array of string
i know the simplest way is :
string[] animals = {"cat", "Dog", "Lizard", "Goat", "Mouse", "Cow"};
string MyAnimal = "Dog";
bool myAnimalIsValid = false;
foreach (string animal in animals)
{
if (animal == MyAnimal // or animal.Contain(MyAnimal))
{
myAnimalIsValid = true;
}
}
if (myAnimalIsValid)
{
//my code
}
I know there is other way to do that like using Select() or Where()
Do you think there is a good optimized way to do that ?
The simplest approach, so without substring but full-string comparison:
bool myAnimalIsContained = animals.Contains(MyAnimal);
Case insensitive:
bool myAnimalIsContainedOgnoringCase
= animals.Contains(MyAnimal, StringComparer.OrdinalIgnoreCase);
But you want to check if any of the animal-names in the list contains your animal as substring?
Then you can use:
bool myAnimalIsContainedAsSubstring = animals.Any(a => a.Contains(MyAnimal));
Case insensitive:
bool myAnimalIsContainedAsSubstringIgnoringCase
= animals.Any(a => a.IndexOf(MyAnimal, StringComparison.OrdinalIgnoreCase) >=0);

get a value from an object property and assign it to a C # string value [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 2 years ago.
The community is reviewing whether to reopen this question as of 2 years ago.
Improve this question
I have a below code to capture an objects values:
var key = fulfillment.GetType().GetProperties().FirstOrDefault(p => p.Name.ToLower().Contains("operator")).GetValue(fulfillment);
the code return:
the Operator property type is:
[JsonProperty(PropertyName = "operator")]
public object Operator { get; set; }
i want to get the name value of the index 1 -> OMS_OPERATOR_AUTOMATED and assign it to another string variable. How can i do this ?
Final answer after looking at code and data structure the answer was:
var foundOperator = (Dictionary<string, object>) fulfillment.Operator;
var teste = foundOperator["name"];

How to find the sequence number of an enumerator inside an enumeration? [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 7 years ago.
Improve this question
Let's assume that there is an enumeration that contains non-ordered by value enumerators. What is the simplest way to find the sequence number of a given enumerator in the enumeration?
You can't, basically. This is not supported. The order from GetNames and GetValues is defined, but is based on the value. The order from GetFields is officially undefined.
if GetFieldsIndex is not officially defined your Might define it unofficially
enum MyEnum { a = 5, b = 2, c = 4, d = 1, e = 3 }
public static int GetFieldIndex(this Enum value)
{
Type t = value.GetType();
var Fields = t.GetFields();
Fields = Array.FindAll(Fields, f => f.FieldType.Equals(t));
var typedFields = Array.ConvertAll(Fields, f => f.GetValue(null));
return Array.IndexOf(typedFields, value);
}
and then consume in the simple way
MyEnum value = MyEnum.d;
Console.WriteLine(value.GetFieldIndex());

Working with nullable [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
I wanted to minimize if block from below code.Please help me suitable extension method
var v = (from rec in _DataContext.tblCourierMasters
where rec.CourierReceievedDate == dtCourierReceivedDate
&& rec.RegionId == lRegionId
&& rec.PODNumber == strPODNo
select new { rec.TotalCafReceived, rec.ReceiptDoneCount }).FirstOrDefault();
lTPC = (long)v.TotalCafReceived;
if (v.ReceiptDoneCount== null) {
lRDC = -1;
}
else
lRDC = (long)v.ReceiptDoneCount;
You could use the null-coalescing operator:
lDRC = (long)(v.ReceiptDoneCount ?? -1);
So if v.ReceiptDoneCount is null, lDRC will be assigned the value of -1 instead.
Here's a demo.

Whats the "Params" keyword in method parameter? [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 9 years ago.
Improve this question
Whats the "Default" keyword in this method?
public IEnumerable<T> createEmpty(Object key)
{
foreach (var item in MyCollection)
{
T element = new T();
element = default(T);
yield return element;
}
}
You mean the "Default" keyword?
Question already answered here: What does default(object); do in C#?
Thats only a method that returns the default value for that type, for example:
Int32 number = default(Int32); // returns 0
Object myObject = default(Object); // returns null
bool flag = default(bool); // return false

Categories