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 4 years ago.
Improve this question
Console.WriteLine("Unlike my deceased predecessor, A.I./Ai, I do not mind not being human. I am content with what I have.");
Console.ReadKey();
bool AI = false;
if (AI = true)
I want the user to type "AI" and have it set the bool to true if they do.
After reading all the comments I think this is what you mean
Console.WriteLine("Unlike my deceased predecessor, A.I./Ai, I do not mind not being human. I am content with what I have.");
string answer = Console.ReadLine();
bool AI = (answer == "AI");
You want to use Console.ReadLine, not Console.ReadKey. You also should make the check case insensitive in case the user enters Ai or ai.
Console.WriteLine("Unlike my deceased predecessor, A.I./Ai, I do not mind not being human. I am content with what I have.");
string lineRead = Console.ReadLine();
bool AI = "ai".Equals(lineRead, StringComparison.OrdinalIgnoreCase);
if(AI)
{
// ai was selected
}
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 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 7 years ago.
Improve this question
It's probably a stupid question, but anyway.
My problem is that I can't pass uninitialized array, but I don't know if my array need to hold 5 or 30000 elements, eg. so it will be a waist of memory to initialize big array.
Should I use List<T> instead, or?
I've noticed people tend to return array instead of list, which is mutable, and therefore much more convenient, so there must be a performance issue with lists. Is that so?
make it into an 'out' parameter and all should be well:
private void x()
{
string sTestFile = "this is a test";
string[] TestFileWords;
FixConcatString(sTestFile, out TestFileWords);
}
private void FixConcatString(string splayfile, **out** string[] sWordArray)
{
char[] charSeparators = new char[] { '-' };
splayfile = splayfile.ToLower();
splayfile = splayfile.Replace(#"\", " ");
sWordArray = splayfile.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
}
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 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
double similarity = matcher.Match(features1, features2);
if (similarity== ?? ) // What sould i write here
{
Application.Exit();
}
if feature1 and feature2 matches than application should exit please help me
Since Double is a floating point type we usually compare Double using tolerance, e.g.
Double tolerance = 0.001;
// Instead of just features1 == features2
if (Math.Abs(features1 - features2) <= tolerance) {
Application.Exit();
}
You need a boolean not a double.
bool similarity = matcher.Match(features1, features2);
if (similarity)
{
Application.Exit();
}
Make sure your mather.Match method returns a bool.
If you really need matcher.Match to return a double then please share your code with us so we can understand why you need this and then help you.