我是一个菜鸟,我只是在努力,2021正视自己,面对2021!

C#基础系列第七篇

前言: 完全是我在学习过程中记录的笔记,只不过分享一下让很多刚开始学习.net编程的人能够很快的学会C#语言

  1. out和ref

(1) 方法中的变量都是局部变量,使用如果需要保证方法中的数据在外界还能使用,那么可以考虑返回值return,但是return只能返回一个数据,如果考虑多个返回数据,就不行了

class Person
{
	public string State;
}
class Program
{
	static void Main(string[] args)
	{
		Person p;
		Create1(out p); //将p的地址传递给Person
	}
	public static void Create1(out Person p) //表示传递Person类
	{
		p = new Person();
	}
}

(2)可以使用参数返回,out就是参数返回的一种

            1)方法外定义一个变量

            2)方法参数写为"...out 参数名..."

            3)调用,在方法中为这个变量赋值

            4)在方法调用后,方法外的这个变量就包含了方法中赋的值

                static void Main(string[] args)

                   {

                          int num;

                          GetNum(out num);

                          Console.WriteLine(num);

                   }

                   static  void GetNum(out int num)

                   {

                          num = 10;

                   }

     (3)ref同样可以使用这个功能

            1)ref在使用前要赋值

            2)out必须在方法中赋值

     (4)一般使用ref是使用方法修改某些数据以后,要求方法结束后还能使用

     (5)一般使用out是为了使用某个方法为某些变量进行初始化

     (6)ref使用前赋值,out方法中赋值

  1. 一个案例:模拟登陆

(1))返回登录是否成功,如果登录失败,提示用户用户名错误还是密码错误

                 class Program
		{
			static void Main(string[] args)
			{
				while (true)
				{
					//输入用户名密码
					Console.Write("请输入用户名:");
					string uid = Console.ReadLine();
					Console.Write("请输入密码:");
					string pwd = Console.ReadLine();
					string msg;
					//判断
					if (Logining(uid, pwd, out msg))
					{
						Console.WriteLine(msg);
					}
					else
					{
						Console.WriteLine(msg);
					}
				}
			}
			static bool Logining(string uid, string pwd,out string msg)
			{
				if (uid == "admin" && pwd == "123456")
				{
					msg = "登录成功";
					return true;
				}
				else if (uid == "admin")
				{
					msg = "密码错误";
					return false;
				}
				else
				{
					msg="用户名错误";
					return false;
				}
			}
		}
  1. 可变参数

(1) 如果参数是一个数组,那么在传参的时候必须定义一个数组出来

         class Program

            {

                   static void Main(string[] args)

                   {

                          int[] nums = { 1, 2, 3, 5, 4, 65, 76, 786, 8, 9, 89 };

                          GetArrayList(nums);

                          GetArrayList(); //传递一个长度为0的数组

                          GetArrayList(1, 2, 3, 5, 4, 65, 76, 7, 34, 65, 76, 86, 8, 9, 89);

                          Console.ReadKey();

                   }

                   public static void GetArrayList(params int[] nums)

                   {

                          Console.WriteLine(nums.Length);

                   }

            }

(2) 如果方法有多个参数params只能放在最后,并且方法中只允许有一个params数组

            static void Main(string[] args)

        {

            int[] nums = { 1, 2, 3, 5, 4, 65, 76, 786, 8, 9, 89 };

            OutPut(5, 23, 43, 54, 54, 65);

            Console.ReadKey();

        }

        //用一个方法完成数组的输出,指定输出多少项

        public static void OutPut(int count, params int[] nums)

        {

            for (int i = 0; i < count; i++)

            {

                Console.Write("{0} ", nums[i]);

            }

        }

        //public static void OutPut(int n1, int n2, int n3, int n4, int n5,int n6)

        //{

        //}

  1. 重载的方法提供转换的实现

(1) 定义隐式转换使用关键字  implicit

 (2)定义显示转换使用关键字  explicit

 (3)转换顺序和定义顺序相同

            变量1=变量2;

            对应参数 类型1(类型2 value);

(4)语法

            [访问修饰符] static [implicitlexplcit] operator 目标类型(待转类型 value)

            {

                   目标类型 o=new 目标类型();

            //赋值操作,显示转换中可以将赋值操作作用checked{..}块包起来,如放不下就异常

                   return 0;

            }

class Program
	{
		static void Main(string[] args)
		{
			Perosn p = new Perosn();
			p.Age = 23;
			int num = (int)p;
			//Console.WriteLine(p);
			Console.WriteLine(num);
		}
	}
class Perosn
{
	int age;
	public int Age
	{
		get { return age; }
		set { age = value; }
	}
	//public static implicit operator int(Perosn value)
	//{
	        //    int n = value.age;
	        //    return n;
	//}
	public static explicit operator int(Perosn value)
	{
		int n = value.age;
		return n;
	}
}
posted @ 2012-10-04 15:39  Kencery  阅读(1727)  评论(1编辑  收藏  举报
友情链接:初心商城