亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關于foreach使用詳解的情況,求解釋~

關于foreach使用詳解的情況,求解釋~

喵喔喔 2021-12-01 11:07:56
這個實例中定義了兩個屬性,看不太明白是怎么回事兒,以前沒用過foreach循環,請詳解,謝謝……using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;public partial class Diary : System.Web.UI.Page{public int ArrayIndex{get {return Convert.ToInt32(ViewState["ArrayIndex"]);//從ViewState中讀取ArrayIndex值}set{ViewState["ArrayIndex"]=value;}}public string[] Juarnal{get{return (string[])ViewState["Juarnal"];}set{ViewState["Juarnal"] = value;}}protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){ViewState["ArrayIndex"] = 0;ViewState["Juarnal"] = new string[31];}}protected void Button1_Click(object sender, EventArgs e){this.Label1Error.Text = "";string Str = this.TextBoxDiary.Text;if (Str != ""){if (ArrayIndex < Juarnal.Length){Juarnal[ArrayIndex] = Str;//賦值ArrayIndex = ArrayIndex + 1;this.Label1Error.Text = "成功";}else{this.Label1Error.Text = "不能超過31天";}}else{this.Label1Error.Text = "你還沒有寫日記";}}protected void Button2_Click(object sender, EventArgs e){this.Label1Error.Text = "";this.LabelShow.Text = "";foreach (string j in Juarnal){if (j != null){this.LabelShow.Text += j + "<br/>";}}}}主要是get 和set搞不明白ViewState["ArrayIndex"]=value;value在這里的意義是什么,看了好多例子,全是這一個參數值
查看完整描述

2 回答

?
ABOUTYOU

TA貢獻1812條經驗 獲得超5個贊

這是迭代的應用,為什么能用迭代,對于繼承了IEnumerable接口的對象 譬如數組,集合,泛型類都可以使用迭代
foreach (string j in Juarnal)
表示 迭代字符串數組Juarnal的項,j是字符串

查看完整回答
反對 回復 2021-12-05
?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

foreach底層也是采用的迭代器實現的。主要是用來遍歷。例子List<String>
list=new
ArrayList<>(String);list.add("aa");.....//添加值略然后遍歷for(String
temp:list){System.out.println(temp);}這樣就可以把list里面的值全輸出出來

查看完整回答
反對 回復 2021-12-05
  • 2 回答
  • 0 關注
  • 293 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號