首页 技术 正文
技术 2022年11月12日
0 收藏 364 点赞 2,904 浏览 2935 个字

饮水思源:金老师的自学网站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 = ;
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("") + Convert.ToInt32("");
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 = { , , };
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

四、简易图片浏览器

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

        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 *= ;
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-); // => True
Console.ReadKey();
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918