C# 01 Primitive Types and Expressions

 

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 = 255;

number = number + 1;  //0
  • 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 = 1;
int b = a;
  • explicit type conversion (casting)
int c = 1;
byte d = (byte)c;
  • conversion between non-compatible types
string e = "100";
int f = int.Parse(e)

var a = "1234";
int b = Convert.ToInt32(a);

 

C# Operators

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

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

 

posted @ 2017-12-10 07:09  jarodli  阅读(225)  评论(0编辑  收藏  举报