C# 课后练习题

17.  XAF框架下的导航按钮

using System;
using System.Collections.Generic;
using System.Line;
using System.Text;
using System.Threading.Tasks;

namespace Tutorial_XAF.Module.BussinessObjects
{
	[XAFDisplayName("测试")]
	[NavigationItem("这是导航")]

	public class TestBo:BaseObject
	{
		public TestBo(Session session):base(session)
		{
			
		}	
		[XAFDisplayName("名称")]
		public string FName{get;set;}
	}	
}

18.颜色案例

using System;

namespace demo18
{
	class program
	{
		foreach(var x in classes)
		{
			if(x.members.contains())
			{
				var member = x.members[];
				member.backColor = Color.Red;
			}	
		}
			
	}


}

19. 方法调用

using System;

namespace CalculatorApplication
{
	class NumberManipulator
	{
		public void getValue(out int x)	
		{
			int temp = 5;
			x = temp;	
		}
		static void Main(string[] args)
		{
			NumberMainpulator n = new NumberManipulator();
			/*局部变量定义*/
			int a = 100;
			Console.WriteLine("在方法调用之前,a的值:{0}",a);
			/*调用函数来获取值*/
			n.getValue(out a);
			
			Console.WriteLine("在方法调用之后,a的值:{0}",a);
			Console.ReadLine();
		}
		
	}	
}

20. 可空数据类型的方法

/*演示了可空数据类型的用法*/

using System;
namespace CalculatorApplication
{
   class NullablesAtShow
   {
      static void Main(string[] args)
      {
         int? num1 = null;
         int? num2 = 45;
         double? num3 = new double?();
         double? num4 = 3.14157;
         
         bool? boolval = new bool?();

         // 显示值
         
         Console.WriteLine("显示可空类型的值: {0}, {1}, {2}, {3}",
                            num1, num2, num3, num4);
         Console.WriteLine("一个可空的布尔值: {0}", boolval);
         Console.ReadLine();

      }
   }
}

 21./*
    您也可以使用类的实例从另一个类中调用其他类的公有方法。
    例如,方法 FindMax 属于 NumberManipulator 类,
    您可以从另一个类 Test 中调用它。
*/


using System;

namespace CalculatorApplication
{
    class NumberManipulator
    {
        public int FindMax(int num1, int num2)
        {
            /* 局部变量声明 */
            int result;

            if (num1 > num2)
                result = num1;
            else
                result = num2;

            return result;
        }
    }
    class Test
    {
        static void Main(string[] args)
        {
            /* 局部变量定义 */
            int a = 100;
            int b = 200;
            int ret;
            NumberManipulator n = new NumberManipulator();
            //调用 FindMax 方法
            ret = n.FindMax(a, b);
            Console.WriteLine("最大值是: {0}", ret );
            Console.ReadLine();

        }
    }
}

22. 声明、赋值、访问数组

/*
	声明、赋值、访问数组
*/
using System;
namespace ArrayApplication
{
   class MyArray
   {
      static void Main(string[] args)
      {
         int []  n = new int[10]; /* n 是一个带有 10 个整数的数组 */
         int i,j;


         /* 初始化数组 n 中的元素 */        
         for ( i = 0; i < 10; i++ )
         {
            n[ i ] = i + 100;
         }

         /* 输出每个数组元素的值 */
         for (j = 0; j < 10; j++ )
         {
            Console.WriteLine("Element[{0}] = {1}", j, n[j]);
         }
         Console.ReadKey();
      }
   }
}

 23.使用foreach循环遍历数组

/*
	使用foreach循环遍历数组
*/
using System;

namespace ArrayApplication
{
   class MyArray
   {
      static void Main(string[] args)
      {
         int []  n = new int[10]; /* n 是一个带有 10 个整数的数组 */


         /* 初始化数组 n 中的元素 */        
         for ( int i = 0; i < 10; i++ )
         {
            n[i] = i + 100;
         }

         /* 输出每个数组元素的值 */
         foreach (int j in n )
         {
            int i = j-100;
            Console.WriteLine("Element[{0}] = {1}", i, j);
         }
         Console.ReadKey();
      }
   }
}

 24./*
    在C#中,可以使用字符数组来表示字符串,
    但更常见的做法是使用string关键字来声明一个字符串变量,
    string关键字是System.String类的别名
*/



using System;

namespace StringApplication
{
    class Program
    {
        static void Main(string[] args)
        {
           //字符串,字符串连接
            string fname, lname;
            fname = "Rowan";
            lname = "Atkinson";

            string fullname = fname + lname;
            Console.WriteLine("Full Name: {0}", fullname);

            //通过使用 string 构造函数
            char[] letters = { 'H', 'e', 'l', 'l','o' };
            string greetings = new string(letters);
            Console.WriteLine("Greetings: {0}", greetings);

            //方法返回字符串
            string[] sarray = { "Hello", "From", "Tutorials", "Point" };
            string message = String.Join(" ", sarray);
            Console.WriteLine("Message: {0}", message);

            //用于转化值的格式化方法
            DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
            string chat = String.Format("Message sent at {0:t} on {0:D}", 
            waiting);
            Console.WriteLine("Message: {0}", chat);
            Console.ReadKey() ;
        }
    }
}

 25.String字符串

/*
	通过给String变量指定一个字符串
	通过使用String类构造函数
	通过使用字符串串联运算符(+)
	通过检索属性或调用一个返回字符串的方法
	通过格式化方法来转换一个值或对象为它的字符串表示形式
*/
using System;

namespace StringApplication
{
    class Program
    {
        static void Main(string[] args)
        {
           //字符串,字符串连接
            string fname, lname;
            fname = "Rowan";
            lname = "Atkinson";

            string fullname = fname + lname;
            Console.WriteLine("Full Name: {0}", fullname);

            //通过使用 string 构造函数
            char[] letters = { 'H', 'e', 'l', 'l','o' };
            string greetings = new string(letters);
            Console.WriteLine("Greetings: {0}", greetings);

            //方法返回字符串
            string[] sarray = { "Hello", "From", "Tutorials", "Point" };
            string message = String.Join(" ", sarray);
            Console.WriteLine("Message: {0}", message);

            //用于转化值的格式化方法
            DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
            string chat = String.Format("Message sent at {0:t} on {0:D}", waiting);
            Console.WriteLine("Message: {0}", chat);
            Console.ReadKey();
        }
    }
}

@www.runoob.com 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值