ylbtech-LanguageSamples-PartialTypes(部分类型)

ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-PartialTypes(部分类型)

 

1.A,示例(Sample) 返回顶部

“分部类型”示例

本示例演示了如何使用允许在两个或更多 C# 文件中定义类或结构的分部类型。这就允许多个程序员并行处理一个类的不同部分,并将复杂类的不同方面保存在不同的文件中。

安全说明

提供此代码示例是为了阐释一个概念,它并不代表最安全的编码实践,因此不应在应用程序或网站中使用此代码示例。对于因将此代码示例用于其他用途而出现的偶然或必然的损害,Microsoft 不承担任何责任。

在 Visual Studio 中生成并运行“分部类型代码”示例

  • 在“调试”菜单中,单击“开始执行(不调试)”。

从命令行生成并运行“分部类型代码”示例

  1. 使用“更改目录(cd)”命令转到“PartialTypes”目录。

  2. 键入以下命令:

    csc PartialTypes.cs
    
    PartialTypes
1.B,示例代码(Sample Code)返回顶部

1.B.1, CharPartialPrivate.cs

ylbtech-LanguageSamples-PartialTypes(部分类型)
// 版权所有(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 >= '0' && ch <= '9')

                return true;

            return false;

        }

    }

}
View Code

1.B.2, ChartPartialPublic.cs

ylbtech-LanguageSamples-PartialTypes(部分类型)
// 版权所有(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 = 0;

            foreach (char ch in str)

            {

                // IsAlphabetic 是在 CharTypesPrivate.cs 中定义的

                if (IsAlphabetic(ch))

                    count++;

            }

            return count;

        }

        public static int CountNumericChars(string str)

        {

            int count = 0;

            foreach (char ch in str)

            {

                // IsNumeric 是在 CharTypesPrivate.cs 中定义的

                if (IsNumeric(ch))

                    count++;

            }

            return count;

        }



    }

}
View Code

1.B.3, PartialTypes.cs

ylbtech-LanguageSamples-PartialTypes(部分类型)
// 版权所有(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 != 1)

            {

                Console.WriteLine("One argument required.");

                return;

            }



            // CharValues 是一个分部类 -- 该分部类有两个方法

            // 是在 CharTypesPublic.cs 中定义的,另有两个方法是在

            // CharTypesPrivate.cs 中定义的。

            int aCount = CharValues.CountAlphabeticChars(args[0]);

            int nCount = CharValues.CountNumericChars(args[0]);

            

            Console.Write("The input argument contains {0} alphabetic and {1} numeric characters", aCount, nCount);

        }

    }

}
View Code

1.B.4,

1.C,下载地址(Free Download)返回顶部

 

warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

你可能感兴趣的:(language)