码迷,mamicode.com
首页 > 其他好文 > 详细

Primitive Types and Expressions

时间:2017-12-10 11:06:05      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:ast   nbsp   ica   gic   method   convert   str   short   cas   

 

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

标签:ast   nbsp   ica   gic   method   convert   str   short   cas   

原文地址:http://www.cnblogs.com/jarodli/p/8016168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!