Grade me?

Started by
11 comments, last by Paragon123 10 years, 4 months ago

I appreciate your feedback. I'd assume using if statements would solve that, but I haven't made it that far yet. I worked with them a bit in JavaScript when I took web programming a bit ago.

I'm not sure yet what the C# equivalent of NaN is but I'd go with using


if (variable = NaN)
    Console.WriteLine("Not a number.");

or something in that ballpark. That's all stuff I've barely seen in action though.

Again, I really, truly appreciate the feedback!

Not sure if that was just an example, orif you actually use that code snippet, but if you do, be sure too use "equal to" == instead of "assign operator" =.

leftHandValue == rightHandValue "is the left had value equal to the right hand value?"

leftHandValue = rightHandValue "assign the right hand value to the left hand value"

you probably know this, but just to make it clear.

C# doesn't allow assignments within comparison blocks. That code will cause a compilation error.

I did not know that.. thanks for sharing.. guess I haven't done that typo in C# yet ;) (probably thanks too the asweome intellisense of VS rather than me screwing up the syntax.. i know that happens way too often anyway xD )

Advertisement

C# doesn't allow assignments within comparison blocks. That code will cause a compilation error.


This is not entirely true. C# allows assignments in comparisons. However, the comparison expression must result in a boolean (which is what catches most of the typos). This means you can still be affected by the typo, but only with boolean variables:


bool b = something;
bool c = somethingElse;
if (b = c)
    Console.WriteLine("Hi"); // This is printed if c is true, regardless of what b was.

With c# I wouldn't concatenate strings the way you are doing it.

For example:

String name="MyName"

Console.WriteLine("Hello, "+name);

should be

Console.WriteLine("Hello, {0}",name);

It tends to be easier to read and easier to modify later.

If WriteLine didn't have that capability built into it try:

Console.WriteLine(String.Format("Hello, {0}",name));

If you are building a string for storage use a StringBuilder

StringBuilder sb=new StringBuilder();

sb.AppendFormat("Hello, {0}",name);

Console.Writeline(sb.ToString());

In C#, each time you concatenate a string it creates a new instance, then copies the two contatenations.

String C="A"+"B"+"C"

creates a String "A" and a String "B", then a new String and copies "A" and "B" to make "AB", then a string "C" then a new String and copies "AB" and "C" to make "ABC"

A StringBuilder dynamically modifies the memory size and avoids excessive copies.

If it is a simple concatenation and you don't need a full on StringBuilder Consider,

String.Concatenate("A","B","C")

For string comparisons, remember

if (String.IsNullOrEmpty(MyString)) Console.Writeline("No text in string")

and use

If (String.Compare(Str1,Str2,true)==0) Console.Writeline("The text is the same, but the casing may be different")

Don't use <type> v = <type>.Parse(String)

use

<type> v;

if (!<type>.TryParse(String, out v)) v=<default>;

-or-

if(!<type>.TryParse(String, out v)) throw new exception("Invalid data.");

This topic is closed to new replies.

Advertisement