《visual c#.net网络核心编程》学习笔记系列之:参数

ContractedBlock.gif ExpandedBlockStart.gif params关键字 #region params关键字
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////params关键字是可以指定在参数数目可变处采用参数的方法参数,(在参数的数目可变时使用)
InBlock.gif        
///在方法声明中的params关键字之后不允许任何其他参数
InBlock.gif        
///并且在方法声明中只允许一个params关键字
InBlock.gif        
///下面的例子说明
ExpandedSubBlockEnd.gif        
///建立一个form,添加一个RichTextBox控件,一个butten控件,一个TextBox控件

InBlock.gif        //RichTextBox richTextBox1 = new RichTextBox();
InBlock.gif
     
InBlock.gif        
private void UseParams(params string[] list)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int i = 0; i < list.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                richTextBox2.AppendText(list[i] 
+ "\r\n");
InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif   
InBlock.gif        
private void UseParams1(params object[] list)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int i = 0; i < list.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                richTextBox2.AppendText((
object)list[i].ToString()+"\r\n");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int b = 0;
InBlock.gif            UseParams(
"how","are","you");
InBlock.gif            UseParams1(b,
"2","3","4","5","6");
InBlock.gif            UseParams1(textBox1.Text);
InBlock.gif            textBox1.Text 
= "";
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

None.gif
ContractedBlock.gifExpandedBlockStart.gif        
ref关键字 #region ref关键字
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////方法参数中的ref关键字可以传递参数值
InBlock.gif        
///即在方法内部对参数的任何改变都会影响到外部变量
InBlock.gif        
///也就是说,外部变量的值会跟着改变
ExpandedSubBlockEnd.gif        
///在定义方法和使用方法时,都要加上ref

InBlock.gif        private void testRef(ref string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            str 
= "how are you.";
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void testNoRef(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            str 
= "how are you.";
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
private void button2_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string myStr = "good study.";
InBlock.gif            richTextBox2.AppendText(
"原始字符串为:"+myStr+"\r\n");
InBlock.gif            testRef(
ref myStr);
InBlock.gif            richTextBox2.AppendText(
"使用Ref时将原始字符串变为:" + myStr + "\r\n");
InBlock.gif            
InBlock.gif            testNoRef(myStr);
InBlock.gif            richTextBox2.AppendText(
"testNoRef方法不使用ref时,原始字符串仍为:"+myStr +"\r\n");
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

None.gif
None.gif
ContractedBlock.gifExpandedBlockStart.gif        
out 关键字 #region out 关键字
InBlock.gif        
private string testOut(out char i)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            i 
= 'a';
InBlock.gif            
return "good study!";
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button3_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
char j;//不需初始化
InBlock.gif
            string getReturn = testOut(out j);
InBlock.gif            richTextBox2.AppendText(
"方法执行的返回值是:"+getReturn+"\r\n");
InBlock.gif            richTextBox2.AppendText(
"返回out参数的值为:"+j.ToString()+"\r\n");
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

None.gif

转载于:https://www.cnblogs.com/sopper/archive/2007/03/07/667132.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值