c# 學(xué)習(xí)筆記2

C# 類型轉(zhuǎn)換
類型轉(zhuǎn)換從根本上說是類型鑄造,或者說是把數(shù)據(jù)從一種類型轉(zhuǎn)換為另一種類型。在 C# 中,類型鑄造有兩種形式

  • 隱式類型轉(zhuǎn)換 - 這些轉(zhuǎn)換是 C# 默認(rèn)的以安全方式進(jìn)行的轉(zhuǎn)換, 不會(huì)導(dǎo)致數(shù)據(jù)丟失。例如,從小的整數(shù)類型轉(zhuǎn)換為大的整數(shù)類型,從派生類轉(zhuǎn)換為基類。
  • 顯式類型轉(zhuǎn)換 - 顯式類型轉(zhuǎn)換,即強(qiáng)制類型轉(zhuǎn)換。顯式轉(zhuǎn)換需要強(qiáng)制轉(zhuǎn)換運(yùn)算符,而且強(qiáng)制轉(zhuǎn)換會(huì)造成數(shù)據(jù)丟失。
using System;

namespace ConsoleApp2
{
    class Program5
    {
        static void Main(string[] args)
        {
            double d = 5673.69;
            string str = "公里數(shù)";
            // 隱式類型轉(zhuǎn)換
            string ss = str + d;
            Console.WriteLine(ss);

            int i;
            // 顯示強(qiáng)制轉(zhuǎn)換  double -- int
            i = (int) d;
            Console.WriteLine(i);  
        }
    }
}

C# 類型轉(zhuǎn)換方法

C# 提供了下列內(nèi)置的類型轉(zhuǎn)換方法:



using System;

namespace ConsoleApp2
{
    class Program6
    {
        static void Main(string[] args)
        {
            int i = 75;
            float f = 5933.443f;
            double d = 32423423.423432;
            bool b = true;
            Console.WriteLine(i.ToString());
            Console.WriteLine(f.ToString());
            Console.WriteLine(d.ToString());
            Console.WriteLine(b.ToString());
        }
    }
}

常量

常量是固定值,程序執(zhí)行期間不會(huì)改變。常量可以是任何基本數(shù)據(jù)類型,比如整數(shù)常量、浮點(diǎn)常量、字符常量或者字符串常量,還有枚舉常量。

常量可以被當(dāng)作常規(guī)的變量,只是它們的值在定義后不能被修改

  • 整數(shù)常量

整數(shù)常量可以是十進(jìn)制、八進(jìn)制或十六進(jìn)制的常量。前綴指定基數(shù):0x 或 0X 表示十六進(jìn)制,0 表示八進(jìn)制,沒有前綴則表示十進(jìn)制。

整數(shù)常量也可以有后綴,可以是 U 和 L 的組合,其中,U 和 L 分別表示 unsigned 和 long。后綴可以是大寫或者小寫,多個(gè)后綴以任意順序進(jìn)行組合。

這里有一些整數(shù)常量的實(shí)例:

212         /* 合法 */
215u        /* 合法 */
0xFeeL      /* 合法 */
078         /* 非法:8 不是一個(gè)八進(jìn)制數(shù)字 */
032UU       /* 非法:不能重復(fù)后綴 */

以下是各種類型的整數(shù)常量的實(shí)例:

85         /* 十進(jìn)制 */
0213       /* 八進(jìn)制 */
0x4b       /* 十六進(jìn)制 */
30         /* int */
30u        /* 無符號(hào) int */
30l        /* long */
30ul       /* 無符號(hào) long *

浮點(diǎn)常量
一個(gè)浮點(diǎn)常量是由整數(shù)部分、小數(shù)點(diǎn)、小數(shù)部分和指數(shù)部分組成。您可以使用小數(shù)形式或者指數(shù)形式來表示浮點(diǎn)常量。

這里有一些浮點(diǎn)常量的實(shí)例

3.14159       /* 合法 */
314159E-5L    /* 合法 */
510E          /* 非法:不完全指數(shù) */
210f          /* 非法:沒有小數(shù)或指數(shù) */
.e55          /* 非法:缺少整數(shù)或小數(shù) */

使用浮點(diǎn)形式表示時(shí),必須包含小數(shù)點(diǎn)、指數(shù)或同時(shí)包含兩者。使用指數(shù)形式表示時(shí),必須包含整數(shù)部分、小數(shù)部分或同時(shí)包含兩者。有符號(hào)的指數(shù)是用 e 或 E 表示的。

字符常量

字符常量是括在單引號(hào)里,例如,'x',且可存儲(chǔ)在一個(gè)簡單的字符類型變量中。一個(gè)字符常量可以是一個(gè)普通字符(例如 'x')、一個(gè)轉(zhuǎn)義序列(例如 '\t')或者一個(gè)通用字符(例如 '\u02C0')。

在 C# 中有一些特定的字符,當(dāng)它們的前面帶有反斜杠時(shí)有特殊的意義,可用于表示換行符(\n)或制表符 tab(\t)。在這里,列出一些轉(zhuǎn)義序列碼:


字符串常量

字符串常量是括在雙引號(hào) "" 里,或者是括在 @"" 里。字符串常量包含的字符與字符常量相似,可以是:普通字符、轉(zhuǎn)義序列和通用字符

使用字符串常量時(shí),可以把一個(gè)很長的行拆成多個(gè)行,可以使用空格分隔各個(gè)部分。

這里是一些字符串常量的實(shí)例。下面所列的各種形式表示相同的字符串。

string a = "hello, world";                  // hello, world
string b = @"hello, world";               // hello, world
string c = "hello \t world";               // hello     world
string d = @"hello \t world";               // hello \t world
string e = "Joe said \"Hello\" to me";      // Joe said "Hello" to me
string f = @"Joe said ""Hello"" to me";   // Joe said "Hello" to me
string g = "\\\\server\\share\\file.txt";   // \\server\share\file.txt
string h = @"\\server\share\file.txt";      // \\server\share\file.txt
string i = "one\r\ntwo\r\nthree";
string j = @"one
two
three";

定義常量
常量是使用 const 關(guān)鍵字來定義的 。定義一個(gè)常量的語法如下:

const <data_type> <constant_name> = value;
 static void Main(string[] args)
        {
            const int a = 100;
            a = 990;//報(bào)錯(cuò)
            Console.WriteLine(a);
        }

運(yùn)算符

算術(shù)運(yùn)算符

下表顯示了 C# 支持的所有算術(shù)運(yùn)算符。假設(shè)變量 A 的值為 10,變量 B 的值為 20,則:



using System;
using System.Security.Cryptography.X509Certificates;

namespace ConsoleApp2
{
    public class Program7
    {
        static void Main(string[] args)
        {
            int a = 10;
            int b = 20;
            int c;
            c = a/b; //整除
            Console.WriteLine("c = {0}", c); //0
            // 前置++, 和 后置 ++ 都是將變量進(jìn)行自增一次
            a++;
            ++a;
            Console.WriteLine("a = {0}", a );//12
            // 前置++是先自增然后在參與運(yùn)算, 和 后置 ++ 先參與運(yùn)算然后在自增
            c = a++ * 10 + 2;
            Console.WriteLine("c = {0}", c);//122
            Console.WriteLine("a = {0}", a );//13
            c = ++a * 10 + 2;
            Console.WriteLine("c = {0}", c); //142
            Console.WriteLine("a = {0}", a ); //14
            
            Console.WriteLine("a = {0}", ++a);// 15
            Console.WriteLine("a = {0}", a++);// 15
            Console.WriteLine("a = {0}", a);// 16
            Console.WriteLine(a/3); //3
            Console.WriteLine(a/3.0); //3.333

        }
    }
}

關(guān)系運(yùn)算符

using System;
using System.Security.Cryptography.X509Certificates;

namespace ConsoleApp2
{
    public class Program8
    {
        static void Main(string[] args)
        {
            int a = 10;
            int b = 20;
            Console.WriteLine(a == b); // False
            Console.WriteLine(a != b); // True
            Console.WriteLine(a <= b);
            Console.WriteLine(a < b);
            Console.WriteLine(a > b);
            Console.WriteLine(a >= b);
            // b = b + 20;
            b += 20;
            Console.WriteLine(b);
        }
    }
}

邏輯運(yùn)算符



using System;

namespace ConsoleApp2
{
    public class Program9
    {
        static void Main(string[] args)
        {
            bool a = true;
            bool b = false;
            int c = 10;
            int d = 20;
            int e = 30;
            // 與
            Console.WriteLine(a && b); //  false
            Console.WriteLine(a || b); //  true
            Console.WriteLine( !a ); //  false
            Console.WriteLine((c <= d) && (d <= e));//  true
            Console.WriteLine((c > d) && (d <= e)); // false
            Console.WriteLine((c > d) || (d <= e)); //  true
            Console.WriteLine(!((c > d) || (d <= e))); //  False
            Console.WriteLine(!(c > d) || (d <= e)); //  true
            Console.WriteLine(!(c < d) && (d <= e)); //  true
        }
        
    }
}

位運(yùn)算符



假設(shè)如果 A = 60,且 B = 13,現(xiàn)在以二進(jìn)制格式表示,它們?nèi)缦滤荆?/p>

A = 0011 1100

B = 0000 1101


A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A = 1100 0011




using System;

namespace ConsoleApp2
{
    public class Program10
    {
        static void Main(string[] args)
        {
            int a = 60; /* 60 = 0011 1100 */  
            int b = 13; /* 13 = 0000 1101 */
            int c;
            c = a & b;  /* 12 = 0000 1100 */
            Console.WriteLine(c);
            c = a | b;   /* 61 = 0011 1101 */
            Console.WriteLine(c);
            c = a ^ b;  /* 49 = 0011 0001 */
            Console.WriteLine(c);
            c = ~a ;  /* -61 = 1100 0011 */
            Console.WriteLine(c);
            c = ~b ;  /* -61 = 1111 0010 */
            Console.WriteLine(c);
            c = a << 2;  /* 240 = 1111 000 */
            Console.WriteLine(c);
            c = a >> 2;  /* 15 = 0000 1111 */
            Console.WriteLine(c);
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容