Skip to content

Instantly share code, notes, and snippets.

@daneb
Created June 21, 2019 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daneb/c30ff2e9ad9c122ea12af3ba3532b159 to your computer and use it in GitHub Desktop.
Save daneb/c30ff2e9ad9c122ea12af3ba3532b159 to your computer and use it in GitHub Desktop.
Type System Safe for C#
[Fact]
public void ThisIsSafe()
{
int a = 5;
int b = a + 2; //OK
bool test = true;
// Error. Operator '+' cannot be applied to operands of type 'int' and 'bool'.
int c = a + test;
}
[Fact]
public void ThisIsUnSafe()
{
var integers = new ArrayList();
integers.Add(1);
integers.Add(2);
integers.Add("3"); // **PROBLEM**
for (int i = 0; i < integers.Count; ++i)
{
int integer = (int)integers[i];
// do something
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment