C sharp #002# 结构化编程基础

饮水思源:金老师的自学网站C# Guide

一、变量与数据类型

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}
type 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();
        }
    }
}

二、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();
        }
    }
}

三、C#控制台程序编程技巧

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

四、简易图片浏览器

        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("操作已取消");
            }
        }

五、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();
        }
    }
}
原文地址:https://www.cnblogs.com/xkxf/p/10791378.html