C# 01 Primitive Types and Expressions

时间:2021-09-21 19:28:09

Class

  • Data or Attributes
    • state of the application
  • Methods or Functions
    • have behavior

Namespace

  • is a container for related classes

Assembly (DLL or EXE)

  • is a container for related namespaces
  • is a file which can either be a EXE or  a DLL

Application

  • include one or more assemblies

Primitive Types (C# type)

  • Integral Number
    • byte (1byte, Max is 255)
    • short
    • int
    • long
  • Real Numbers
    • float (4byte)
    • double
    • decimal
  • Character
    • char (2byte)
  • Boolean
    • bool (1byte)
    • good practice
      • Prefix Boolean names with is or has
      • For example: isOver18, isCitizen, isEligible

Something tricky about real  numbers

  • data type is double by default
  • wrong
float number = 1.2;

decimal number = 1.2;
  • right
float number = 1.2f;

decimal number = 1.2m;

Non-Primitive Types

  • String
  • Array
  • Enum
  • Class

Overflowing

  • for example
byte number = ;

number = number + ;  //
  • if you use check keyword, overflow will not happen and instead the program will throw an exception (But don't use it in real world )

Scope

  • where a variable / constant has meaning

Type Conversion

  • implicit type conversion
byte a = ;
int b = a;
  • explicit type conversion (casting)
int c = ;
byte d = (byte)c;
  • conversion between non-compatible types
string e = "";
int f = int.Parse(e) var a = "";
int b = Convert.ToInt32(a);

C# Operators

  • Arithmetic Operators
    • +
    • -
    • *
    • /
var a = ;
var b = ;
Console.WriteLine(a+b); //
Console.WriteLine((float)a / (float)b); // 3.333333
    • %
    • Postfix increment
int a = ;
int b = a++; //b = 1, a = 2;
    • Prefix increment
int a = ;

int b = ++a; //b = 2, a = 2;
  • Comparison Operators
    • ==
    • !=
    • >
    • >=
    • <
    • <=
  • Assignment
    • =
    • +=
    • -=
    • *=
    • /=
  • Logical Operator
    • &&
      • And
      • double ampersand
      • a&&b
    • ||
      • Or
      • double vertical line
      • a||b
    • !
      • Not
      • exclamation mark
      • !a
  • Bitwise Operators
    • &  And
    • |  Or

Comments

  • Single-line Comment
// Here is a single-line comment
  • Multi-line Comments
/*

Here is a multi-line

comment

*/
  • When to use comments
    • to explain whys, hows, constrains, etc
    • Not explain the whats.
      • comment do not explain what the code is doing