C# OpenCvSharp DNN 部署yolov5不规则四边形目标检测

本文主要是介绍C# OpenCvSharp DNN 部署yolov5不规则四边形目标检测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

效果

模型信息

项目

代码

下载


C# OpenCvSharp DNN 部署yolov5不规则四边形目标检测

效果

模型信息

Inputs
-------------------------
name:images
tensor:Float[1, 3, 1024, 1024]
---------------------------------------------------------------

Outputs
-------------------------
name:output
tensor:Float[1, 64512, 11]
---------------------------------------------------------------

项目

代码

using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Reflection;
using System.Windows.Forms;

namespace OpenCvSharp_DNN_Demo
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";

        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;

        float confThreshold;
        float nmsThreshold;
        float objThreshold;

        float[,] anchors = new float[3, 6] {
                                           {31, 30, 28, 49, 50, 31},
                                           {46, 45, 58, 58, 74, 74},
                                           {94, 94, 115, 115, 151, 151}
                                           };

        float[] stride = new float[3] { 8.0f, 16.0f, 32.0f };

        string modelpath;

        int inpHeight;
        int inpWidth;

        List<string> class_names;
        int num_class;

        Net opencv_net;
        Mat BN_image;

        Mat image;
        Mat result_image;

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;

            pictureBox1.Image = null;
            pictureBox2.Image = null;
            textBox1.Text = "";

            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            confThreshold = 0.5f;
            nmsThreshold = 0.5f;
            objThreshold = 0.5f;

            modelpath = "model/best.onnx";

            inpHeight = 1024;
            inpWidth = 1024;

            opencv_net = CvDnn.ReadNetFromOnnx(modelpath);

            class_names = new List<string>();
            StreamReader sr = new StreamReader("model/coco.names");
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                class_names.Add(line);
            }
            num_class = class_names.Count();

            image_path = "test_img/1.png";
            pictureBox1.Image = new Bitmap(image_path);

        }

        float sigmoid(float x)
        {
            return (float)(1.0 / (1 + Math.Exp(-x)));
        }

        Mat ResizeImage(Mat srcimg, out int newh, out int neww, out int top, out int left)
        {
            int srch = srcimg.Rows, srcw = srcimg.Cols;
            top = 0;
            left = 0;
            newh = inpHeight;
            neww = inpWidth;
            Mat dstimg = new Mat();
            if (srch != srcw)
            {
                float hw_scale = (float)srch / srcw;
                if (hw_scale > 1)
                {
                    newh = inpHeight;
                    neww = (int)(inpWidth / hw_scale);
                    Cv2.Resize(srcimg, dstimg, new OpenCvSharp.Size(neww, newh), 0, 0, InterpolationFlags.Area);
                    left = (int)((inpWidth - neww) * 0.5);
                    Cv2.CopyMakeBorder(dstimg, dstimg, 0, 0, left, inpWidth - neww - left, BorderTypes.Constant);
                }
                else
                {
                    newh = (int)(inpHeight * hw_scale);
                    neww = inpWidth;
                    Cv2.Resize(srcimg, dstimg, new OpenCvSharp.Size(neww, newh), 0, 0, InterpolationFlags.Area);
                    top = (int)((inpHeight - newh) * 0.5);
                    Cv2.CopyMakeBorder(dstimg, dstimg, top, inpHeight - newh - top, 0, 0, BorderTypes.Constant);
                }
            }
            else
            {
                Cv2.Resize(srcimg, dstimg, new OpenCvSharp.Size(neww, newh));
            }
            return dstimg;
        }

        float IoU(BoxInfo polya, BoxInfo polyb, int max_w, int max_h)
        {
            List<List<OpenCvSharp.Point>> poly_array0 = new List<List<OpenCvSharp.Point>>();
            List<List<OpenCvSharp.Point>> poly_array1 = new List<List<OpenCvSharp.Point>>();
            poly_array0.Add(polya.pts);
            poly_array1.Add(polyb.pts);

            Mat _poly0 = Mat.Zeros(max_h, max_w, MatType.CV_8UC1);
            Mat _poly1 = Mat.Zeros(max_h, max_w, MatType.CV_8UC1);
            Mat _result = new Mat();

            List<List<OpenCvSharp.Point>> _pts0 = new List<List<OpenCvSharp.Point>>();
            List<int> _npts0 = new List<int>();

            foreach (var item in poly_array0)
            {
                if (item.Count < 3)//invalid poly
                    return -1f;

                _pts0.Add(item);
                _npts0.Add(item.Count);

            }

            List<List<OpenCvSharp.Point>> _pts1 = new List<List<OpenCvSharp.Point>>();
            List<int> _npts1 = new List<int>();

            foreach (var item in poly_array1)
            {
                if (item.Count < 3)//invalid poly
                    return -1f;

                _pts1.Add(item);
                _npts1.Add(item.Count);

            }

            Cv2.FillPoly(_poly0, _pts0, new Scalar(1));
            Cv2.FillPoly(_poly1, _pts1, new Scalar(1));

            Cv2.BitwiseAnd(_poly0, _poly1, _result);

            int _area0 = Cv2.CountNonZero(_poly0);
            int _area1 = Cv2.CountNonZero(_poly1);
            int _intersection_area = Cv2.CountNonZero(_result);
            float _iou = (float)_intersection_area / (float)(_area0 + _area1 - _intersection_area);
            return _iou;
        }

        void nms(List<BoxInfo> input_boxes, int max_w, int max_h)
        {
            input_boxes.Sort((a, b) => { return a.score > b.score ? -1 : 1; });

            bool[] isSuppressed = new bool[input_boxes.Count];

            for (int i = 0; i < input_boxes.Count(); ++i)
            {
                if (isSuppressed[i]) { continue; }
                for (int j = i + 1; j < input_boxes.Count(); ++j)
                {
                    if (isSuppressed[j]) { continue; }
                    float ovr = IoU(input_boxes[i], input_boxes[j], max_w, max_h);
                    if (ovr >= nmsThreshold)
                    {
                        isSuppressed[j] = true;
                    }
                }
            }

            for (int i = isSuppressed.Length - 1; i >= 0; i--)
            {
                if (isSuppressed[i])
                {
                    input_boxes.RemoveAt(i);
                }
            }

        }

        private unsafe void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }
            textBox1.Text = "检测中,请稍等……";
            pictureBox2.Image = null;
            Application.DoEvents();

            image = new Mat(image_path);

            int newh = 0, neww = 0, padh = 0, padw = 0;
            Mat dstimg = ResizeImage(image, out newh, out neww, out padh, out padw);

            BN_image = CvDnn.BlobFromImage(dstimg, 1 / 255.0, new OpenCvSharp.Size(inpWidth, inpHeight), new Scalar(0, 0, 0), true, false);

            //配置图片输入数据
            opencv_net.SetInput(BN_image);

            //模型推理,读取推理结果
            Mat[] outs = new Mat[3] { new Mat(), new Mat(), new Mat() };
            string[] outBlobNames = opencv_net.GetUnconnectedOutLayersNames().ToArray();

            dt1 = DateTime.Now;

            opencv_net.Forward(outs, outBlobNames);

            dt2 = DateTime.Now;

            int num_proposal = outs[0].Size(1);
            int nout = outs[0].Size(2);

            if (outs[0].Dims > 2)
            {
                outs[0] = outs[0].Reshape(0, num_proposal);
            }

            float ratioh = 1.0f * image.Rows / newh, ratiow = 1.0f * image.Cols / neww;

            float* pdata = (float*)outs[0].Data;

            List<BoxInfo> generate_boxes = new List<BoxInfo>();

            int row_ind = 0;

            for (int n = 0; n < 3; n++)
            {

                int num_grid_x = (int)(inpWidth / stride[n]);
                int num_grid_y = (int)(inpHeight / stride[n]);

                for (int q = 0; q < 3; q++)    //anchor
                {
                    float anchor_w = anchors[n, q * 2];
                    float anchor_h = anchors[n, q * 2 + 1];
                    for (int i = 0; i < num_grid_y; i++)
                    {
                        for (int j = 0; j < num_grid_x; j++)
                        {
                            float box_score = sigmoid(pdata[8]);
                            if (box_score > objThreshold)
                            {

                                Mat scores = outs[0].Row(row_ind).ColRange(9, 9 + num_class);
                                double minVal, max_class_socre;
                                OpenCvSharp.Point minLoc, classIdPoint;
                                // Get the value and location of the maximum score
                                Cv2.MinMaxLoc(scores, out minVal, out max_class_socre, out minLoc, out classIdPoint);

                                int class_idx = classIdPoint.X;
                                max_class_socre = sigmoid((float)max_class_socre) * box_score;
                                if (max_class_socre > confThreshold)
                                {
                                    List<OpenCvSharp.Point> pts = new List<OpenCvSharp.Point>();
                                    for (int k = 0; k < 8; k += 2)
                                    {
                                        float x = (pdata[k] + j) * stride[n];  //x
                                        float y = (pdata[k + 1] + i) * stride[n];   //y
                                        x = (x - padw) * ratiow;
                                        y = (y - padh) * ratioh;
                                        pts.Add(new OpenCvSharp.Point(x, y));
                                    }

                                    Rect r = Cv2.BoundingRect(pts);

                                    generate_boxes.Add(new BoxInfo(pts, (float)max_class_socre, class_idx));
                                }
                            }
                            row_ind++;
                            pdata += nout;
                        }
                    }

                }

            }

            nms(generate_boxes, image.Cols, image.Rows);

            result_image = image.Clone();

            for (int ii = 0; ii < generate_boxes.Count; ++ii)
            {
                int idx = generate_boxes[ii].label;

                for (int jj = 0; jj < 4; jj++)
                {
                    Cv2.Line(result_image, generate_boxes[ii].pts[jj], generate_boxes[ii].pts[(jj + 1) % 4], new Scalar(0, 0, 255), 2);
                }

                string label = class_names[idx] + ":" + generate_boxes[ii].score.ToString("0.00");

                int xmin = (int)generate_boxes[ii].pts[0].X;
                int ymin = (int)generate_boxes[ii].pts[0].Y - 10;

                Cv2.PutText(result_image, label, new OpenCvSharp.Point(xmin, ymin - 5), HersheyFonts.HersheySimplex, 0.75, new Scalar(0, 0, 255), 1);
            }

            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
            textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
        }

        private void pictureBox2_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox2.Image);
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox1.Image);
        }
    }
}

using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Reflection;
using System.Windows.Forms;namespace OpenCvSharp_DNN_Demo
{public partial class frmMain : Form{public frmMain(){InitializeComponent();}string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";string image_path = "";DateTime dt1 = DateTime.Now;DateTime dt2 = DateTime.Now;float confThreshold;float nmsThreshold;float objThreshold;float[,] anchors = new float[3, 6] {{31, 30, 28, 49, 50, 31},{46, 45, 58, 58, 74, 74},{94, 94, 115, 115, 151, 151}};float[] stride = new float[3] { 8.0f, 16.0f, 32.0f };string modelpath;int inpHeight;int inpWidth;List<string> class_names;int num_class;Net opencv_net;Mat BN_image;Mat image;Mat result_image;private void button1_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;if (ofd.ShowDialog() != DialogResult.OK) return;pictureBox1.Image = null;pictureBox2.Image = null;textBox1.Text = "";image_path = ofd.FileName;pictureBox1.Image = new Bitmap(image_path);image = new Mat(image_path);}private void Form1_Load(object sender, EventArgs e){confThreshold = 0.5f;nmsThreshold = 0.5f;objThreshold = 0.5f;modelpath = "model/best.onnx";inpHeight = 1024;inpWidth = 1024;opencv_net = CvDnn.ReadNetFromOnnx(modelpath);class_names = new List<string>();StreamReader sr = new StreamReader("model/coco.names");string line;while ((line = sr.ReadLine()) != null){class_names.Add(line);}num_class = class_names.Count();image_path = "test_img/1.png";pictureBox1.Image = new Bitmap(image_path);}float sigmoid(float x){return (float)(1.0 / (1 + Math.Exp(-x)));}Mat ResizeImage(Mat srcimg, out int newh, out int neww, out int top, out int left){int srch = srcimg.Rows, srcw = srcimg.Cols;top = 0;left = 0;newh = inpHeight;neww = inpWidth;Mat dstimg = new Mat();if (srch != srcw){float hw_scale = (float)srch / srcw;if (hw_scale > 1){newh = inpHeight;neww = (int)(inpWidth / hw_scale);Cv2.Resize(srcimg, dstimg, new OpenCvSharp.Size(neww, newh), 0, 0, InterpolationFlags.Area);left = (int)((inpWidth - neww) * 0.5);Cv2.CopyMakeBorder(dstimg, dstimg, 0, 0, left, inpWidth - neww - left, BorderTypes.Constant);}else{newh = (int)(inpHeight * hw_scale);neww = inpWidth;Cv2.Resize(srcimg, dstimg, new OpenCvSharp.Size(neww, newh), 0, 0, InterpolationFlags.Area);top = (int)((inpHeight - newh) * 0.5);Cv2.CopyMakeBorder(dstimg, dstimg, top, inpHeight - newh - top, 0, 0, BorderTypes.Constant);}}else{Cv2.Resize(srcimg, dstimg, new OpenCvSharp.Size(neww, newh));}return dstimg;}float IoU(BoxInfo polya, BoxInfo polyb, int max_w, int max_h){List<List<OpenCvSharp.Point>> poly_array0 = new List<List<OpenCvSharp.Point>>();List<List<OpenCvSharp.Point>> poly_array1 = new List<List<OpenCvSharp.Point>>();poly_array0.Add(polya.pts);poly_array1.Add(polyb.pts);Mat _poly0 = Mat.Zeros(max_h, max_w, MatType.CV_8UC1);Mat _poly1 = Mat.Zeros(max_h, max_w, MatType.CV_8UC1);Mat _result = new Mat();List<List<OpenCvSharp.Point>> _pts0 = new List<List<OpenCvSharp.Point>>();List<int> _npts0 = new List<int>();foreach (var item in poly_array0){if (item.Count < 3)//invalid polyreturn -1f;_pts0.Add(item);_npts0.Add(item.Count);}List<List<OpenCvSharp.Point>> _pts1 = new List<List<OpenCvSharp.Point>>();List<int> _npts1 = new List<int>();foreach (var item in poly_array1){if (item.Count < 3)//invalid polyreturn -1f;_pts1.Add(item);_npts1.Add(item.Count);}Cv2.FillPoly(_poly0, _pts0, new Scalar(1));Cv2.FillPoly(_poly1, _pts1, new Scalar(1));Cv2.BitwiseAnd(_poly0, _poly1, _result);int _area0 = Cv2.CountNonZero(_poly0);int _area1 = Cv2.CountNonZero(_poly1);int _intersection_area = Cv2.CountNonZero(_result);float _iou = (float)_intersection_area / (float)(_area0 + _area1 - _intersection_area);return _iou;}void nms(List<BoxInfo> input_boxes, int max_w, int max_h){input_boxes.Sort((a, b) => { return a.score > b.score ? -1 : 1; });bool[] isSuppressed = new bool[input_boxes.Count];for (int i = 0; i < input_boxes.Count(); ++i){if (isSuppressed[i]) { continue; }for (int j = i + 1; j < input_boxes.Count(); ++j){if (isSuppressed[j]) { continue; }float ovr = IoU(input_boxes[i], input_boxes[j], max_w, max_h);if (ovr >= nmsThreshold){isSuppressed[j] = true;}}}for (int i = isSuppressed.Length - 1; i >= 0; i--){if (isSuppressed[i]){input_boxes.RemoveAt(i);}}}private unsafe void button2_Click(object sender, EventArgs e){if (image_path == ""){return;}textBox1.Text = "检测中,请稍等……";pictureBox2.Image = null;Application.DoEvents();image = new Mat(image_path);int newh = 0, neww = 0, padh = 0, padw = 0;Mat dstimg = ResizeImage(image, out newh, out neww, out padh, out padw);BN_image = CvDnn.BlobFromImage(dstimg, 1 / 255.0, new OpenCvSharp.Size(inpWidth, inpHeight), new Scalar(0, 0, 0), true, false);//配置图片输入数据opencv_net.SetInput(BN_image);//模型推理,读取推理结果Mat[] outs = new Mat[3] { new Mat(), new Mat(), new Mat() };string[] outBlobNames = opencv_net.GetUnconnectedOutLayersNames().ToArray();dt1 = DateTime.Now;opencv_net.Forward(outs, outBlobNames);dt2 = DateTime.Now;int num_proposal = outs[0].Size(1);int nout = outs[0].Size(2);if (outs[0].Dims > 2){outs[0] = outs[0].Reshape(0, num_proposal);}float ratioh = 1.0f * image.Rows / newh, ratiow = 1.0f * image.Cols / neww;float* pdata = (float*)outs[0].Data;List<BoxInfo> generate_boxes = new List<BoxInfo>();int row_ind = 0;for (int n = 0; n < 3; n++){int num_grid_x = (int)(inpWidth / stride[n]);int num_grid_y = (int)(inpHeight / stride[n]);for (int q = 0; q < 3; q++)    //anchor{float anchor_w = anchors[n, q * 2];float anchor_h = anchors[n, q * 2 + 1];for (int i = 0; i < num_grid_y; i++){for (int j = 0; j < num_grid_x; j++){float box_score = sigmoid(pdata[8]);if (box_score > objThreshold){Mat scores = outs[0].Row(row_ind).ColRange(9, 9 + num_class);double minVal, max_class_socre;OpenCvSharp.Point minLoc, classIdPoint;// Get the value and location of the maximum scoreCv2.MinMaxLoc(scores, out minVal, out max_class_socre, out minLoc, out classIdPoint);int class_idx = classIdPoint.X;max_class_socre = sigmoid((float)max_class_socre) * box_score;if (max_class_socre > confThreshold){List<OpenCvSharp.Point> pts = new List<OpenCvSharp.Point>();for (int k = 0; k < 8; k += 2){float x = (pdata[k] + j) * stride[n];  //xfloat y = (pdata[k + 1] + i) * stride[n];   //yx = (x - padw) * ratiow;y = (y - padh) * ratioh;pts.Add(new OpenCvSharp.Point(x, y));}Rect r = Cv2.BoundingRect(pts);generate_boxes.Add(new BoxInfo(pts, (float)max_class_socre, class_idx));}}row_ind++;pdata += nout;}}}}nms(generate_boxes, image.Cols, image.Rows);result_image = image.Clone();for (int ii = 0; ii < generate_boxes.Count; ++ii){int idx = generate_boxes[ii].label;for (int jj = 0; jj < 4; jj++){Cv2.Line(result_image, generate_boxes[ii].pts[jj], generate_boxes[ii].pts[(jj + 1) % 4], new Scalar(0, 0, 255), 2);}string label = class_names[idx] + ":" + generate_boxes[ii].score.ToString("0.00");int xmin = (int)generate_boxes[ii].pts[0].X;int ymin = (int)generate_boxes[ii].pts[0].Y - 10;Cv2.PutText(result_image, label, new OpenCvSharp.Point(xmin, ymin - 5), HersheyFonts.HersheySimplex, 0.75, new Scalar(0, 0, 255), 1);}pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";}private void pictureBox2_DoubleClick(object sender, EventArgs e){Common.ShowNormalImg(pictureBox2.Image);}private void pictureBox1_DoubleClick(object sender, EventArgs e){Common.ShowNormalImg(pictureBox1.Image);}}
}

下载

源码下载

这篇关于C# OpenCvSharp DNN 部署yolov5不规则四边形目标检测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/488871

相关文章

C#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

C#实现添加/替换/提取或删除Excel中的图片

《C#实现添加/替换/提取或删除Excel中的图片》在Excel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更加美观,下面我们来看看如何在C#中实现添加/替换/提取或删除E... 在Excandroidel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更

C#实现系统信息监控与获取功能

《C#实现系统信息监控与获取功能》在C#开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途,比如在系统性能优化工具中,需要实时读取CPU、GPU资源信息,本文将详细介绍如何使用C#来实现... 目录前言一、C# 监控键盘1. 原理与实现思路2. 代码实现二、读取 CPU、GPU 资源信息1.

python管理工具之conda安装部署及使用详解

《python管理工具之conda安装部署及使用详解》这篇文章详细介绍了如何安装和使用conda来管理Python环境,它涵盖了从安装部署、镜像源配置到具体的conda使用方法,包括创建、激活、安装包... 目录pytpshheraerUhon管理工具:conda部署+使用一、安装部署1、 下载2、 安装3

在C#中获取端口号与系统信息的高效实践

《在C#中获取端口号与系统信息的高效实践》在现代软件开发中,尤其是系统管理、运维、监控和性能优化等场景中,了解计算机硬件和网络的状态至关重要,C#作为一种广泛应用的编程语言,提供了丰富的API来帮助开... 目录引言1. 获取端口号信息1.1 获取活动的 TCP 和 UDP 连接说明:应用场景:2. 获取硬

C#使用HttpClient进行Post请求出现超时问题的解决及优化

《C#使用HttpClient进行Post请求出现超时问题的解决及优化》最近我的控制台程序发现有时候总是出现请求超时等问题,通常好几分钟最多只有3-4个请求,在使用apipost发现并发10个5分钟也... 目录优化结论单例HttpClient连接池耗尽和并发并发异步最终优化后优化结论我直接上优化结论吧,

SpringBoot使用Apache Tika检测敏感信息

《SpringBoot使用ApacheTika检测敏感信息》ApacheTika是一个功能强大的内容分析工具,它能够从多种文件格式中提取文本、元数据以及其他结构化信息,下面我们来看看如何使用Ap... 目录Tika 主要特性1. 多格式支持2. 自动文件类型检测3. 文本和元数据提取4. 支持 OCR(光学

C#使用yield关键字实现提升迭代性能与效率

《C#使用yield关键字实现提升迭代性能与效率》yield关键字在C#中简化了数据迭代的方式,实现了按需生成数据,自动维护迭代状态,本文主要来聊聊如何使用yield关键字实现提升迭代性能与效率,感兴... 目录前言传统迭代和yield迭代方式对比yield延迟加载按需获取数据yield break显式示迭

c# checked和unchecked关键字的使用

《c#checked和unchecked关键字的使用》C#中的checked关键字用于启用整数运算的溢出检查,可以捕获并抛出System.OverflowException异常,而unchecked... 目录在 C# 中,checked 关键字用于启用整数运算的溢出检查。默认情况下,C# 的整数运算不会自

C#实现获得某个枚举的所有名称

《C#实现获得某个枚举的所有名称》这篇文章主要为大家详细介绍了C#如何实现获得某个枚举的所有名称,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下... C#中获得某个枚举的所有名称using System;using System.Collections.Generic;usi