Primitive Types and Expressions

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于Primitive Types and Expressions的主要内容,如果未能解决你的问题,请参考以下文章

Java 基本数据类型 Primitive Data Types

Java 基本数据类型 Primitive Data Types

Oracle OCA J2SE7 Cetificate - Rules for Primitive Types

[TypeScript] Represent Non-Primitive Types with TypeScript’s object Type

[Unity官方文档翻译]Primitive and Placeholder Objects Unity原生3D物体教程

Reports local variable of wrapper type though all usages allowed to be primitive and count of unnece