C sharp #002# 結構化編程基礎

飲水思源:金老師的自學網站C# Guide編程

 

1、變量與數據類型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int intValue = 100;
            long longValue = 100L;
            double doubleValue = 100.5d;
            float floatValue = 100.5f;

            Console.WriteLine("==========GetType()===============");
            Console.WriteLine(intValue.GetType());
            Console.WriteLine(longValue.GetType());
            Console.WriteLine(doubleValue.GetType());
            Console.WriteLine(floatValue.GetType());

            Console.WriteLine(typeof(int));
            Console.WriteLine(typeof(int) == intValue.GetType()); // typeof可用於檢測變量是不是特定類型
            // => System.Int32
            // => System.Int64
            // => System.Double
            // => System.Single

            // => System.Int32
            // => True
            Console.ReadKey();
            Console.WriteLine("==========String or string?===============");
            Console.WriteLine(typeof(String) == typeof(string)); // => True
            // 可是string仍然屬於引用類型,生存於「堆」中
            Console.ReadKey();

            Console.WriteLine("==========var===============");
            var v1 = "hello"; // 可是用var關鍵字定義變量時,c#能夠根據右邊的賦值,自動推斷類型
            var v2 = new Dictionary<string, List<int>>();
            Console.WriteLine("type of v1: {0}\ntype of v2: {1}", v1.GetType(), v2.GetType());
            Console.ReadKey();

            Console.WriteLine("==========sizeof===============");
            Console.WriteLine("int所佔字節的大小" + sizeof(int)); // => 4
            Console.WriteLine("long所佔字節的大小" + sizeof(long)); // => 8
            long v3 = 22L;
            int v4 = (int) v3; // 所佔字節大的賦給所佔字節小的須要類型轉換
            Console.ReadKey();

            // string轉化爲數值類型
            v4 = int.Parse("100") + Convert.ToInt32("200");
            Console.WriteLine("v4: " + v4); // 隱式調用了v4.ToString()
            Console.ReadKey();
        }
    }
}

 

2、C#中For each的寫法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var names = new List<string> { "<name>", "Ana", "Felipe" };
            foreach (var name in names)
            {
                Console.WriteLine($"Hello {name.ToUpper()}!");
            }

            int[] numbers = { 1, 2, 3 };
            foreach (var number in numbers)
            {
                Console.WriteLine(number);
            }

            Console.ReadKey();
        }
    }
}

 

3、C#控制檯程序編程技巧

https://docs.microsoft.com/en-us/dotnet/api/system.console?view=netframework-4.8#methodsc#

 

4、簡易圖片瀏覽器

        private void button1_Click(object sender, EventArgs e)
        {
            loadPic();
        }

        /// <summary>
        /// 加載圖片
        /// </summary>
        private void loadPic()
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                MessageBox.Show("即將爲你打開圖片:" + openFileDialog1.FileName);
                pictureBox1.ImageLocation = openFileDialog1.FileName;
            }
            else
            {
                MessageBox.Show("操做已取消");
            }
        }

 

5、BigInteger以及浮點數的比較

using System;
using System.Numerics;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 計算機使用固定的位數來保存數值,所以,能處理的數值大小是有限的,
            // 當要處理的數值超過了這一範圍時,計算機將會自動截斷數值的二進制表
            // 示爲它所能處理的最多位數。

            // 須要添加對System.Numerics程序集的引用
            BigInteger bi = long.MaxValue;
            bi *= 2;
            Console.WriteLine(long.MaxValue);
            Console.WriteLine(bi);
            // => 9223372036854775807
            // => 18446744073709551614
            Console.ReadKey();

            double i = 0.0001;
            double j = 0.00010000000000000001;
            Console.WriteLine(i == j); // => True
            // 計算機不能精確地表達浮點數(特殊形式的除外),所以,當
            // 須要比較兩個浮點數是否相等時,應該比較其差的絕對值是否
            // 在某個容許範圍以內便可,沒法作到像數學那樣的精確比較。
            Console.WriteLine(Math.Abs(i - j) < 1e-10); // => True
            Console.ReadKey();
        }
    }
}
相關文章
相關標籤/搜索