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
时间: 2024-10-08 10:35:15

Primitive Types and Expressions的相关文章

Java中的原始类型(Primitive Types)与引用类型(Reference Values)

Java虚拟机可以处理的类型有两种,一种是原始类型(Primitive Types),一种是引用类型(Reference Types). 与之对应,也存在有原始值(Primitive Values)和引用值(Reference Values)两种类型的数值可用于变量赋值.参数传递.方法返回和运算操作. 原始类型与值 Java虚拟机支持的原始数据类型包括数值类型.布尔类型和returnAddress类型. 数值类型包括:整数类型和浮点类型. 整数类型包括: 1.byte 2.short 3.int

Prefer Domain- Specific Types to Primitive Types

Prefer Domain- Specific Types to Primitive Types Einar Landre ON SEPTEMBER 23, 1999, the $327.6 million Mars Climate Orbiter was lost while entering orbit around Mars due to a software error back on Earth. The error was later called the metric mix-up

Primitive Types in Go

Introduction As a strong  type static language, Go has many primitive types we will care about. In first level, they can be divided into two groups: non-collection and collections(e.g. array, slice, map). In this article we will talk three different

Oracle OCA J2SE7 Cetificate - Rules for Primitive Types

Any bigger than an int can NEVER be assigned to an int or anything smaller than int (byte, char, or short) without explicit cast. Constant values up to int can be assigned (without cast) to variables of lesser size (e.g. short to byte) if the values

[C++] Variables and Basic Types

Getting Started compile C++ program source $ g++ -o prog grog1.cc run C++ program $ ./prog The library, iostream, define four IO object: cin, cout, cerr, clog. std::cout << "hello world" << std::endl; The result of the output operato

C# 2012 step by step 学习笔记8 CHAPTER 9 Creating Value types with enumerations and Structures

C# 2012 step by step 学习笔记8 CHAPTER 9 Creating Value types with enumerations and Structures things about 1. Declare an enumeration type. 2. Create and use an enumeration type. 3. Declare a structure type. 4. Create and use a structure type. 5. Explain

.NET 中,编译器直接支持的数据类型称为基元类型(primitive type).基元类型和.NET框架类型(FCL)中的类型有直接的映射关系.

.NET 中,编译器直接支持的数据类型称为基元类型(primitive type).基元类型和.NET框架类型(FCL)中的类型有直接的映射关系. The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single. https://msdn.microsoft.com/zh-cn/library/s

Android jni/ndk编程二:jni数据类型转换(primitive,String,array)

一.数据类型映射概述 从我们开始jni编程起,就不可能避开函数的参数与返回值的问题.java语言的数据类型和c/c++有很多不同的地方,所以我们必须考虑当在java层调用c/c++函数时,怎么正确的把java的参数传给c/c++函数,怎么正确的从c/c++函数获取正确的函数返回值:反之,当我们在c/c++中使用java的方法或属性时,如何确保数据类型能正确的在java和c/c++之间转换. 回顾我们上一篇文章中的那个c函数: #include <stdio.h> #include <jn

Core Java Volume I — 3.3. Data Types

3.3. Data TypesJava is a strongly typed language(强类型语音). This means that every variable must have a declared type(每个变量都必须声明类型). There are eight primitive types in Java(Java有8种原始类型). Four of them are integer types; two are floatingpoint number types;