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
Related
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);
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"];
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());
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.
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 9 years ago.
Improve this question
Which one is correct and WHY
in both examples we have a function that determines if a certain string is valid...
(using some other function that's not defined here)
private Validator = new Validator();
public Boolean IsValid(String foo)
{
if (Validator.Validate(foo))
{
return true;
}
else
{
return false;
}
}
in the second scenario we have a function that ends with a TRUE statement and with no else.
private Validator = new Validator();
public Boolean IsValid(String foo)
{
if (!Validator.Validate(foo))
{
return false;
}
return true;
}
NOW INB4 please dont say that you can simply do it this way
return Validator.Validate(foo);
How to save a few lines its not what i want to know...but the implications and unknown consecuences ( to me ) of using one method or the other.
Because both IsValid() methods do nothing else they are equivalent.
All 3 are correct. The third is my preference because it's less code and still very readable in this case.
I think that the best solution is:
public bool IsValid(String foo)
{
return (Validator.Validate(foo))? true : false;
}
In addition the conditional expression is easy to understand and it's inline