C#,入门教程(38)——大型工程软件中类(class)修饰词partial的使用方法

上一篇:

C#,入门教程(37)------优秀程序员的修炼之道https://blog.csdn.net/beijinghorn/article/details/125011644

一、大型(工程应用)软件倚重 partial

先说说大型(工程应用)软件对源代码的文件及函数"行"数的基本要求:

(1)每个class文件不要太多行,512行就算多的了;

(2)每个函数的行数也不要太多,256行就不少了;

而实际上,很多 class 却不得不又很多行程序,因而对于(1)而言 partial 就非常重要了。

首先,partial 允许将一个 class 拆分,写到多个 .cs 文件中!

其次,partial 允许大家分工编写 class 的不同部分!

总之,partial 非常有用!

二、一些不太重要的限制

至于下面的一下局限性与限制,大致了解即可:

1、仅适用于类class、接口interface、结构struct,不支持委托delegate、枚举enum;

2、每个部分必须都有修饰符 partial;

3、位于同一个的命名空间namespace;

4、如有部分使用static/abstract/sealed,整个类都被视为static/abstract/sealed;互相不能矛盾;

5、各个部分的基类必须一致;

6、局部类型上的接口具有累加效应。

三、partial 实例

字符串匹配(Pattern Search)有很多算法,各种算法可以写在一个 静态类中。

但应该分别写在不同的 cs 文件,便于维护、协作、管理。

文件1、Legalsoft.Truffer.PatternSearch.KMP.cs

cs 复制代码
using System;
using System.Collections;
using System.Collections.Generic;

namespace Legalsoft.Truffer.Algorithm
{
	/// <summary>
	/// 字符串匹配(模式搜索)算法集锦
	/// </summary>
	public static partial class PatternSearch
	{
		/// <summary>
		/// 字符串匹配的KMP算法
		/// </summary>
		/// <param name="pat"></param>
		/// <param name="txt"></param>
		public static List<int> KMPSearch(string pattern, string text)
		{
			List<int> result = new List<int>();

			int M = pattern.Length;
			int N = text.Length;

			int[] lps = new int[M];
			int j = 0;

			Build_LPS_Array(pattern, M, lps);

			int i = 0;
			while (i < N)
			{
				if (pattern[j] == text[i])
				{
					j++;
					i++;
				}
				if (j == M)
				{
					result.Add(i - j);
					j = lps[j - 1];
				}

				else if (i < N && pattern[j] != text[i])
				{
					if (j != 0)
					{
						j = lps[j - 1];
					}
					else
					{
						i = i + 1;
					}
				}
			}

			return result;
		}

		/// <summary>
		/// 构造 LPS 数组
		/// 最长后缀数组,Longest Proper Suffix 
		/// </summary>
		/// <param name="pattern"></param>
		/// <param name="M"></param>
		/// <param name="lps"></param>
		private static void Build_LPS_Array(string pattern, int M, int[] lps)
		{
			lps[0] = 0;
			int len = 0;
			int i = 1;
			while (i < M)
			{
				if (pattern[i] == pattern[len])
				{
					len++;
					lps[i] = len;
					i++;
				}
				else
				{
					if (len != 0)
					{
						len = lps[len - 1];
					}
					else
					{
						lps[i] = len;
						i++;
					}
				}
			}
		}
	}
}

PatternSearch 类的另外一个 暴力算法,写在:

文件2、Legalsoft.Truffer.PatternSearch.Native.cs

cs 复制代码
using System;
using System.Collections;
using System.Collections.Generic;

namespace Legalsoft.Truffer.Algorithm
{
	/// <summary>
	/// 字符串匹配(模式搜索)算法集锦
	/// </summary>
	public static partial class PatternSearch
	{
		/// <summary>
		/// 字符串匹配的暴力算法
		/// </summary>
		/// <param name="text"></param>
		/// <param name="pattern"></param>
		/// <returns></returns>
		public static List<int> NativeSearch(string text, string pattern)
		{
			int M = pattern.Length;
			int N = text.Length;
			int S = N - M;

			List<int> matchs = new List<int>();
			if (S <= 0) return matchs;
			for (int i = 0; i <= S; i++)
			{
				int j = 0;
				while (j < M)
				{
					if (text[i + j] != pattern[j])
					{
						break;
					}
					j++;
				}

				if (j == M)
				{
					matchs.Add(i);
				}
			}

			return matchs;
		}
	}
}

POWER BY 315SOFT.COM &
TRUFFER.CN

相关推荐
weixin_428005302 小时前
C#调用 AI学习从0开始-第1阶段(基础与工具)-第2天Prompt工程基础
人工智能·学习·c#·prompt
咩图3 小时前
WPF-VisualStudio-C#-Fluent.Ribbon8.0.0学习
c#·wpf·visual studio
加号34 小时前
【C#】WPF基于Halcon 的HWindowControlWPF 控件实现图像缩放、移动
开发语言·c#·wpf
张较瘦_5 小时前
[论文阅读] AI + 软件工程 | 告别意图丢失!基于算法的LLM代码翻译新范式来了
论文阅读·人工智能·软件工程
雪豹阿伟5 小时前
2.C# —— 结构体、类型转换与运算符
c#·上位机
njsgcs7 小时前
c# solidworks GetPartBox无法获得正确实体边界框原因
开发语言·c#·solidworks
rockey6277 小时前
AScript之匿名类型与动态类型
c#·.net·script·eval·expression·动态脚本
99乘法口诀万物皆可变7 小时前
BMS HIL 自动化测试框架方案(基于 CANoe + C# + Excel)
开发语言·c#·excel
祀爱8 小时前
定时任务之BackgroundService的详细教程
后端·c#·asp.net
Soari8 小时前
终结 Vibe Coding(Harness Engineering)!深度拆解 ralph:以交付所有 PRD 为生命周期的自主 AI Agent 闭环
自动化测试·人工智能·软件工程·aiagent·ralph·harnesseng·prd驱动