Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
How to optimize the following C# code?
public class SampleClass
{
public bool IsGreaterThenZero(int i)
{
if (i > 0)
return true;
else
return false;
}
}
Since binary comparison operators, such as >, <, and so on, produce a bool result, ou can return i>0 directly, like this:
public bool IsGreaterThenZero(int i) {
return i > 0;
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a method that needs to convert a string to the generic type:
T GetValue<T>(string name)
{
string item = getstuff(name);
return item converted to T // ????????
}
T could be int or date.
you can use Convert.ChangeType
T GetValue<T>(string name)
{
string item = getstuff(name);
return (T)Convert.ChangeType(item, typeof(T));
}
if you need to limit input types only for int and DateTime, add condition like below
if (typeof(T) != typeof(int) && typeof(T) != typeof(DateTime))
{
// do something with other types
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Ok i Edited post to short version i want to ask about methods in c#: TryGetValue and sorting list by the compare (IComparator).
I need write this methods in Java so I wrote this code. Asking to developers which know both this language is that correct??
1) First question- TryGetValue is that methods wrote in Java make the same?
c#:
Node value;
if (!nodes.TryGetValue(nodeId,out value)) return false;
to Java:
Node value;
if (!nodes.containsKey(nodeId)){
return false;
}else{
value = nodes.get(nodeId);
}
2)And Sorting by the comparator in java work the same?
static int compareNodes(Node n1, Node n2)
{
if (n1.f > n2.f) return 1;
if (n1.f < n2.f) return -1;
return 0;
}
list.Sort(compareNodes);
And this in Java:
#Override
public int compare(Node nodeFirst, Node nodeSecond) {
if (nodeFirst.f > nodeSecond.f)
return 1;
if (nodeFirst.f < nodeSecond.f)
return -1;
return 0;
}
Collections.sort(nodeList, new OpenList());
After just a quick glance over your code, I can't see any real problems with it. No guarantees though.
If you really want to be sure, you should probably create unit-tests to verify the behavior in both C# and in Java. That will require a little more work, but will be a lot safer than depending on reviews here.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I convert the following logic to LINQ?
Flaggedlist is a type of List<string>. request.flagged is a numeric value in a request POCO.
if (request.Flagged == 1)
{
if (!patient.UserFlaggedList.Contains(request.UserId))
{
flaggedList.Add(request.UserId);
}
}
else if (request.Flagged == 0)
{
string usrid = flaggedList.Where(a => a == request.UserId).FirstOrDefault<string>();
flaggedList.Remove(usrid);
}
It can be boiled down to two simple IF statements and the 2nd part shortened further still:
if (request.Flagged == 1 && !patient.UserFlaggedList.Contains(request.UserId))
flaggedList.Add(request.UserId);
if (request.Flagged == 0)
flaggedList.Remove(flaggedList.FirstOrDefault(a => a == request.UserId));
This is pretty condensed and clear, not sure why you'd want to make it more condensed.
If you really want to make it shorter, maybe you can do this:
if (request.Flagged == 1 && !patient.UserFlaggedList.Contains(request.UserId))
{
flaggedList.Add(request.UserId);
}
else if (request.Flagged == 0)
{
flaggedList.Remove(flaggedList.Where(a => a == request.UserId).FirstOrDefault<string>());
}
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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have Json file in string (for example):
#{
"Url": "http://site.com/?q=windows8"
}
How can i take the information after ?q= on c# (windows 8). Sorry for my English.
You can use the querystring.
in Codebehind file
public String q
{
get
{
if (Request.QueryString["q"] == null)
return String.Empty;
return Convert.ToString(Request.QueryString["q"]);
}
}
then use the line below to get the value
var index = ('<%=q%>');
You can do simply this :
string s = "myURL/?q=windows8";
// Loop through all instances of ?q=
int i = 0;
while ((i = s.IndexOf("?q=", i)) != -1)
{
// Print out the substring. Here : windows8
Console.WriteLine(s.Substring(i));
// Increment the index.
i++;
}