Chapter 5 - More About Variables

1. Why need Type Conversion?

Different variables represent data using varying schemes. Thus, it might not be the expected result if the sequence of bits were directly mapped from one value to another of a different type.


2. What are the types of Type Conversion?

2.1 Implicit Conversion

2.1.1 Definition

Implicit conversion requires no explicit work in code.

2.1.2 Where?

Between simple numeric types in assignments or expressions.

2.1.3 When?

The source type's range completely fits inside the range of the target type.

2.2 Explicit Conversion

2.2.1 Definition

Explicit conversion asks the compiler to convert a value from one data type to another and requires extra code.

2.2.2 Format

The explicit conversion format works like a unary operator.

(<destinationType>) <sourceVar>

2.2.3 Where?

Between simple numeric types that bear certain relation to each other.

2.2.4 When?

Must do when implicit conversion does not work.

2.2.5 Overflow

Explicit conversion sometimes may lead to overflow when the source type is out of the range. "checked" keyword can be used to monitor such error.

2.3 Convert Commands

2.3.1 Format

<targetVar> = Convert.ToXXX(<sourceVar>)

2.3.2 Where?

Between simple types that bear certain relation to each other. This method extends the capability of explicit conversion that is only limited to numeric values which can be easily converted in terms of bits inplacement.

2.3.3 Overflow

The overflow checking is always placed inside all these commands.


3. How to use Enumeration?

3.1 Purpose

The simple numeric types define a fixed set of values, which sometimes, require customization.

3.2 Usage

3.2.1 Declare a user-defined type.

enum <typeName>
{
    <value1>,
    <value2>,
    ...
    <valueN>,
}

3.2.2 Use the type to declare variables.

<typeName> <varName>;

3.3 Underlying Type

The enumeration is essentially stored as an underlying type in terms of a simple numeric one which includes byte, sbyte, short, ushort, int, uint, long, and ulong. Each member of the enumeration is mapped to a specific value of that underlying type.

enum <typeName> : <underlyingType>
{
    <value1> = <actualValue1>,
    <value2> = <actualValue2>,
    ...
    <valueN> = <actualValueN>,
}

3.4 Type Conversion

3.4.1 An enumeration can be converted to or from a value of its underlying type.

<enumValue> = (<enumType>) <underlyingValue>;
<underlyingValue> = (<underlyingType>) <enumValue>;

3.4.2 An enumeration can be converted to or from a value of string.

<stringValue> = <enumValue>.ToString();
<underlyingValue> = (<enumType>)Enum.Parse(typeof(<enumType>), <stringValue>);


4. How to use Struct?

4.1 Purpose

Structs enable programmers to define their own data structures composed of several data members including variables.

4.2 Usage

Like enumerations, struct must be defined first and then can be used to declare its variables, because it is a kind of complex type.

4.2.1 Declare a user-defined type.

struct <typeName>
{
    <accessibility> <varType> <varName>;
    ...
}

4.2.2 Use the type to declare variables.

<typeName> <varName> = new <typeName>();

For one thing deserved to be mentioned, the value of the struct is created via "new" grammer.

4.2.3 Access member variables.

<varName>.<memberVarName>


5. How to use Array?

5.1 Definition

An array is an indexed list of variables of the same type which is quite convenient for storing a set of values.

5.2 Usage

Arrays are essentially variables so they need to be declared first before being used.

5.2.1 Declare an array.

<accessibility> <varType>[] <varName>;

5.2.2 Assign a value to an array.

5.2.2.1 Pure Literal Value

The simpliest way to assign a value to an array is providing comma-seperated list of element values. For example:

int[] myArray = {1, 2, 3, 4, 5};

5.2.2.2 Initializer

Initializer can be used if the size of the array which must be provided by a constant value is known. For example:

int[] myArray = new int[5];

5.2.2.3 Initializer + Literal Value

int[] myArray = new int[5] {1, 2, 3, 4, 5};

5.2.3 Access an existing array.

5.2.3.1 Direct Access

myArray[0] = 10;

5.2.3.2 Foreach Loop

The foreach loop can traverse each value in a given array without illegal indexed accessing.

foreach (<elementType> <varName> in <arrayVarName))
{
    ...
}


6. How to manipulate string?

6.1 A string can be treated as a read-only array of char.

6.1.1 indexed access

string myString = "Hello World";
char myChar = myString[0]; //assign 'H'

6.1.2 char array conversion

string myString = "Hello World";
char[] myChar = myString.ToCharArray(); //assign {'H','e','l','l','o',' ','W','o','r','l','d'}

6.2 A string can be converted into upper case or lower case.

6.3 A string can be trimmed of its beginning or ending characters.

6.4 A string can be adjusted to a desired length by padding spaces to its beginning or ending.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值