-
Intro
-
C# vs .NET
-
C#
- is a programming Lang
-
.NET
-
framework for building applications on Windows
- also support diff lang
- VB.NET
- F#
-
.NET
-
consists of
-
CLR (Common Language Runtime)
- translate IL(Intermediate Language) into Native Code
- JIT (Just In Time Compilation)
-
Class Library
- for building applications
- Classes collaborate with each other at runtime
-
Architecture of .NET Applications
-
Application
- consists of building blocks called Classes
-
Class
-
Attributes
- state of application
-
Functions/Methods
- behavior
- execute code
-
Namespace
- way to organize Classes
- container for related classes
-
Assembly
- way of partitioning an application
- container for related namespaces/classes
-
File on disk
- DLL
- Dynamically Link Library
- a file that includes code that can be re-used across different programs
- EXE
- a program that can be executed
-
When application is complied
- compiler builds 1 or more assemblies(depending on how code is partition)
-
Visual Studio
-
Run
- Ctrl + F5
-
Delete line
- Ctrl + X
-
ReSharper
- Alt + Enter
- Get rid of unused "using" namespaces
- Console.WriteLine()
-
Compile
- Ctrl + Shift + B
-
3. Primitive Types & Expressions
-
14. Variables & Constants
-
Variable
- Name given to a storage location in memory
-
Use Camel Case
- Eg.
- firstName
-
Constant
- Immutable value
- const float Pi = 3.14f;
-
Use Pascal Case
- Eg.
- MaxZoom
-
Primitive Types
- byte
- int
- long
- decimal
- char
- bool
-
15. Overflowing
- "checked" keyword
-
Use
- checked {
byte number = 255;
number = number +1; // 0
}
- will throw exception if run
- without "checked", it'll overflow without error returning "0"
- 16. Scope
-
17. Demo
-
Default number with decimal
- double
-
need to append 'f' behind the decimal number
- Eg.
- float total = 123.12f
- Keywords are in blue
-
Code snippet
-
Shortcut for "Console.WriteLine()"
- type "cw", double Tab
- "var" keyword
-
Object Browser
-
"Ctrl + click" on keywords
- doesn't work! -RL
-
Place-holder
-
Eg.
- Console.WriteLine("{0} {1}", byte.MinValue, byte.MaxValue);
- {0}
- 1st place-holder format
-
18. Type Conversion
-
Implicit Type Conversion
- No data lose
-
Eg.
- byte b = 1;
int i = b;
- int i = 1;
float f = i;
-
Explicit Type Conversion(Casting)
- Data will be lost if the "type" is beyond the capacity
-
Eg.
- w/o Casting
- CANNOT Compile
- int i = 1;
byte b = i;
- w/ Casting
- CAN Compile
- int i = 1;
byte b = (byte)i;
- float f = 1.0f;
int i = (int)f;
-
Conversion between Non-compatible types
-
use
- Convert class
- Eg.
- string s = "3";
int i = Convert.ToInt32(s);
- ToByte()
- ToInt16()
- short
- ToInt32()
- int
- ToInt64()
- long
- Parse method
- Eg.
- string s = "3";
int j = int.Parse(s);
-
19. Demo - Type Conversion
-
Exception handling
-
try/catch
- try {
} catch (Exception) {
}
-
20. Operators
-
Arithmetic
-
Add
- +
-
Subtract
- -
-
Multiple
- *
-
Divide
- /
-
Remainder
- %
-
Increment
- ++
-
Decrement
- --
-
Prefix
- Eg.
- int a = 1;
int b = ++a; // a = 2, b = 2
-
Postfix
- Eg.
- int a = 1;
int b = a++; // a = 2, b = 1
-
Comparison
-
Equal
- ==
-
Not Equal
- !=
-
Greater than
- >
-
Greater than or equal to
- >=
-
Less than
- <
-
Less than or equal to
- <=
-
Assignment
-
Assignment
- =
-
Addition
- +=
-
Subtraction
- -=
-
Multiplication
- *=
-
Division
- /=
-
Logical
-
And
- &&
-
Or
- ||
-
Not
- !
-
Bitwise
- for low level Programming
-
And
- &
-
Or
- |
-
21. What are Logical Operators?
-
&&
- ONLY if both are true, it's true
-
||
- If ANY of the both is true, it's true
-
23. Demo - Operators
-
if division of 2 int, return will be int as well
-
Eg.
- var a = 10;
var b = 3;
Console.WriteLine(a / b); // return 3
- var a = 10;
var b = 3;
Console.WriteLine((float)a / (float)b); // return 3.333333
-
changing precedence
-
use
- parenthesis
- ()
-
24.
-
Comments
-
Single-line
- // This is a comment.
-
Multi-line
- /*
This is a comment.
This is a comment too.
*/
-
4. Non-primitive Types
-
26. Classes
- Variables(Fields)
- functions(Methods)
-
Object
- instance of a Class
-
Declaring
-
Access modifier
- Eg.
- public
- "class" keyword
-
identifier
- Eg.
- Person
-
Eg.
- public class Person
{
public string Name;
public void Introduce()
{
Console.WriteLine("Hi, my name is " + Name);
}
}
-
Creating Objects
-
Eg.
- var person = new Person();
person.Name = "Richard";
person.Introduce();
-
Static Modifier
- can directly access the method through the Class itself, w/o creating an Object
-
Eg.
- public class Calculator
{
public static int Add(int x, int y)
{
return x + y;
}
}
int result = Calculator.Add(1, 2);
-
27. Demo - Classes
-
Move class to a file
-
select the class
- Press - Alt + Enter
- or
- Press Ctrl + "."
-
Switch between different Windows
- Ctrl + tab
-
28. Structs
- similar to Classes
-
Eg.
- public struct RgbColor
{
public int Red;
public int Green;
public int Blue;
}
- not frequently used
- use if you want to define a small lightweight object, or else just stick to Classes
- more efficient to define as struct if need to define lots of it
-
29. Arrays
-
What is?
- Data structure to store a collection of variables of the same type
-
Declaring
-
Eg.
- int[] numbers = new int[3];
- int[]
- declaring array
- int[3]
- set size of array
- var numbers = new int[3];
-
Initializing
-
Eg.
- int[] numbers = new int[3] {1, 2, 3};
-
Access Array Elements
-
Eg.
- numbers[1] = 3;
- 30. Demo - Arrays
-
31. Strings
-
What is?
- sequence of characters
-
Eg.
- "Hello World"
-
How to create?
-
Using
- Literals
- Eg.
- string firstName = "Richard";
- Concatenation
- Eg.
- string name = firstName + " " + lastName;
- Format
- Eg.
- string name = string.Format("{0} {1}", firstName, lastName);
- {0}
- Place holder
- Join
- Eg.
- var numbers = new int[3] {1, 2, 3};
string list = string.Join(",", numbers); // return 1,2,3
-
String Elements
-
Eg.
- string name = "Richard";
char firstChar = name[0]; // return 'R'
-
Strings are Immutable
-
Once Created, CANNOT change!
- name[0] = 'r';
-
Escape Characters
-
Use
- \n
- New Line
- \t
- Tab
- \\
- Backslash
- \'
- Single Quotation Mark
- \"
- Double Quotation Mark
-
Eg.
- string path = "c:\\projects\\project1\\folders";
-
Verbatim Strings
-
Use
- @
-
Eg.
- string path = @"c:\projects\project1\folders";
-
32. Demo - Strings
-
.NET
-
string
- map to
- String
- class
-
int
- map to
- Int32
- struct
-
Eg.
- String firstName = "Richard";
- Can also use "String" keyword instead of "string"
- Have to import "using System"
-
33. Enums
-
What is?
- A set of name/value pairs (constants)
-
When to use?
- you've a number of related constants
-
Eg.
- public enum ShippingMethod
{
RegularAirMail = 1,
RegisteredAirMail = 2,
Express = 3;
}
-
Info
- Internally an Integer
-
How to change to byte?
- public enum ShippingMethod : byte
{
RegularAirMail = 1,
RegisteredAirMail = 2,
Express = 3;
}
-
How to use?
-
Eg.
- var method = ShippingMethod.Express;
-
How to use?
-
Convert to Int
- Cast
- Eg.
- Console.WriteLine((int)method);
- return
- 3
-
Convert to String
- .ToString() method
- Eg.
- var methodStr = ShippingMethod.RegisterdAirMail.ToString();
Console.WriteLine(methodStr);
-
Convert to Enum
- Parse
- Eg.
- var methodName = "RegisterdAirMail";
var shippingMethod = (ShippingMethod)Enum.Parse(typeof(ShippingMethod), methodName);
- return
- "RegisterAirMail"
- 34. Demo - Enums
-
35. Reference Types & Values Types
-
Primitive Types
-
Eg.
- int
- char
- float
- bool
-
Non-primitive Types
-
Eg.
- classes
- structures
- arrays
- map to
- System.Array
- strings
- map to
- System.String
-
2 main types
- for which we create new types
-
What are they?
- Value Types
- Structures
- for
- Primitive types
- Custom structures
- Allocate on stack
- Memory allocation done automatically
- Immediately removed when out of scope
- by Runtime/CLR
- Reference Types
- Classes
- for
- Arrays
- Strings
- Custom classes
- Need to allocate memory
- using "new"
- Memory allocated on
- Heap
- more sustainable
- if object goes out of scope, it won't be removed immediately.
It'll continue to be in heap for a little while.
- Garbage collected by Runtime/CLR
- Copying of reference types & value types are different