ylbtech-LanguageSamples-PartialTypes(部分类型)
2024-09-26 14:41:10
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-PartialTypes(部分类型) |
1.A,示例(Sample) 返回顶部 |
“分部类型”示例
本示例演示了如何使用允许在两个或更多 C# 文件中定义类或结构的分部类型。这就允许多个程序员并行处理一个类的不同部分,并将复杂类的不同方面保存在不同的文件中。
安全说明 |
---|
提供此代码示例是为了阐释一个概念,它并不代表最安全的编码实践,因此不应在应用程序或网站中使用此代码示例。对于因将此代码示例用于其他用途而出现的偶然或必然的损害,Microsoft 不承担任何责任。 |
在 Visual Studio 中生成并运行“分部类型代码”示例
在“调试”菜单中,单击“开始执行(不调试)”。
从命令行生成并运行“分部类型代码”示例
使用“更改目录(cd)”命令转到“PartialTypes”目录。
键入以下命令:
csc PartialTypes.cs
PartialTypes
1.B,示例代码(Sample Code)返回顶部 |
1.B.1, CharPartialPrivate.cs
// 版权所有(C) Microsoft Corporation。保留所有权利。
// 此代码的发布遵从
// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
//
//版权所有(C) Microsoft Corporation。保留所有权利。 using System;
using System.Collections.Generic;
using System.Text; namespace PartialClassesExample
{
// 使用 partial 关键字可以在其他 .cs 文件中定义
// 此类的附加方法、字段和属性。
// 此文件包含 CharValues 定义的私有方法。
partial class CharValues
{
private static bool IsAlphabetic(char ch)
{
if (ch >= 'a' && ch <= 'z')
return true;
if (ch >= 'A' && ch <= 'Z')
return true;
return false;
} private static bool IsNumeric(char ch)
{
if (ch >= '' && ch <= '')
return true;
return false;
}
}
}
1.B.2, ChartPartialPublic.cs
// 版权所有(C) Microsoft Corporation。保留所有权利。
// 此代码的发布遵从
// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
//
//版权所有(C) Microsoft Corporation。保留所有权利。 using System;
using System.Collections.Generic;
using System.Text; namespace PartialClassesExample
{
// 使用 partial 关键字可以在其他 .cs 文件中定义
// 此类的附加方法、字段和属性。
// 此文件包含 CharValues 定义的公共方法。
partial class CharValues
{
public static int CountAlphabeticChars(string str)
{
int count = ;
foreach (char ch in str)
{
// IsAlphabetic 是在 CharTypesPrivate.cs 中定义的
if (IsAlphabetic(ch))
count++;
}
return count;
}
public static int CountNumericChars(string str)
{
int count = ;
foreach (char ch in str)
{
// IsNumeric 是在 CharTypesPrivate.cs 中定义的
if (IsNumeric(ch))
count++;
}
return count;
} }
}
1.B.3, PartialTypes.cs
// 版权所有(C) Microsoft Corporation。保留所有权利。
// 此代码的发布遵从
// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
//
//版权所有(C) Microsoft Corporation。保留所有权利。 using System;
using System.Collections.Generic;
using System.Text; namespace PartialClassesExample
{
class PartialClassesMain
{
static void Main(string[] args)
{
if (args.Length != )
{
Console.WriteLine("One argument required.");
return;
} // CharValues 是一个分部类 -- 该分部类有两个方法
// 是在 CharTypesPublic.cs 中定义的,另有两个方法是在
// CharTypesPrivate.cs 中定义的。
int aCount = CharValues.CountAlphabeticChars(args[]);
int nCount = CharValues.CountNumericChars(args[]); Console.Write("The input argument contains {0} alphabetic and {1} numeric characters", aCount, nCount);
}
}
}
1.B.4,
1.C,下载地址(Free Download)返回顶部 |
作者:ylbtech 出处:http://ylbtech.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
最新文章
- HDU 1075 What Are You Talking About(Trie的应用)
- Tomcat Nginx cluster note
- ios 企业发布
- NetAdvantage 笔记
- F - Truck History - poj 1789
- IdentityServer(12)- 使用 ASP.NET Core Identity
- awk 的 pattern(模式)
- socks-proxy---with ssh
- ESP8266 软件实现 Delta-sigma(ΔΣ)调制器 并通过I2S接口输出编码流
- Java基础 - 线程(一)
- 了解java虚拟机—G1回收器(9)
- centos chroot使用
- 使用ShareSDK完成第三方(QQ、微信、微博)登录和分享
- NodeJS学习笔记六
- Application Loader上传app,一直卡在“正在通过 App Store 进行鉴定”
- C# 使用int.TryParse,Convert.ToInt32,(int)将浮点类型转换整数时的区别
- simulink pi的方法产生锁相环
- ListView实现分页加载(三)实现分页加载
- 【poj Roads in the North】 题解
- 将list集合,元素随机打乱
热门文章
- mac date 和 Linux date实现从指定时间开始循环
- hdu 1269 迷宫城堡(Targin算法)
- [转载] Python itertools模块详解
- Windows7 + OSG3.6 + VS2017 + Qt5.11
- 关于Sphinx中使用 RealTime Index的问题
- echarts官网上的动态加载数据bug被我解决。咳咳/。
- lr_Vugen界面图
- mybatis 报错: Invalid bound statement (not found)
- vmware漏洞之三——Vmware虚拟机逃逸漏洞(CVE-2017-4901)Exploit代码分析与利用
- CodeForces 723F st-Spanning Tree