path | title |
---|---|
/learnings/csharp |
Learnings: Csharp |
- variable declarations
- classes
- casts
- Strings
- properties
- initializing objects
- checked variables
- control flow
- method syntax
- Null Handling
- TODO:
string thing
Parameters in C# are pass by "value": even if the copied value is a copy of an address pointer.
Thus, if you want to assign something to an incoming parameter, you need the ref
keyword
void MyMethod( ref string outString ) {
outString = "hi"
}
string userName = "temp"
MyMethod(ref userName)
Problem: userName
variable needs to be initialized (compiler doesn't know you're just writing to it).
void MyMethod( out string outString ) {
out = "hi"
}
string userName
MyMethod( userName )
NOTE:: method using out parameters must assign all out
parameters!!!
Features:
- single inheritence
public class Employee : Person {
}
Defaults to nonvirtual (???)
For virtual behavior need both virtual
on base class and override
on subclass.
C style casts
@"this is a literal string \ this will show up like a backslash not escape character"
$"indicates that {variables} will be interpreted"
In other words, a property has the behavior of special methods called setters and getters, but the syntax for accessing that behavior is that of a field.
Can initilize fields at object construction time, with handy syntax:
(C# 3.0)
Employee n = new Employee("Ryan", "Wilcox") {
jobTitle = "Software Engineer",
salary = "$"
}
Will throw an exception if you try to overflow the variable type
This can be controlled by compiler OR by checked {} and unchecked {} variable block scopes
public void name(TYPE varName)
"if this value is blank use that one": a ?? b
C# 6.0
varThatCouldBeNull?.something
#preprocessors
Yes, C# has them.
Operations:
- #if
- #define
- #error
- #warning
- #pragma
- #line <-- override what line number is reported by the compiler in case or error or warning (!!!!!!)
Q: Is the following how C# does annotations??
[TestMethod]
public void message() {
1 + 1
}