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

相关推荐
2501_9307077812 分钟前
如何使用C#代码将多张图片整合为一个PDF文档
开发语言·pdf·c#
粟悟饭&龟波功15 分钟前
【软考系统架构设计师】二、绪论
系统架构·软件工程
赵庆明老师16 分钟前
NET 10 中DLL,并发布到NuGet
服务器·c#·.net
时光追逐者41 分钟前
排查 EF 保存数据时提示:Validation failed for one or more entities 的问题
数据库·c#·.net·ef
时光追逐者1 小时前
在 .NET 中将 EF Core 升级到 9.0.5 MySQL 连接提示 get_LockReleaseBehavior
数据库·mysql·c#·.net·ef core
江沉晚呤时1 小时前
使用 C# 和 Semantic Kernel 构建 PDF 向量搜索系统:从文本提取到语义搜索
jvm·人工智能·microsoft·chatgpt·c#
禅道程序猿1 小时前
从标准到落地:ASPICE双V模型在汽车软件工程中的实践路径
汽车·产品运营·项目管理·软件工程·产品经理·敏捷流程
唐青枫1 小时前
LINQ 新时代:CountBy、AggregateBy 深度解析(含对比 GroupBy)
c#·.net
联系QQ 1808095113 小时前
Mitsubishi FX5U伺服机器人程序:包括三菱PLC程序、威纶通触摸屏程序、IO表、材...
软件工程
yue00814 小时前
C# winform自定义控件
开发语言·c#