C#中數組,類型轉換
2024-07-21 02:19:18
供稿:網友
菜鳥學堂:
當數據轉換到asp.net頁面時,大部分都是以文本的形式存在的。有時候為了輸出,單單使用顯示或者隱式轉換都是不行的,就需要本節說到的數據轉換。
字符串輸出:
int intage=21;
string strage=intage.tostring();
轉換datetime值時,可以通過在括號內放置一個可選的參數來指定時間的輸出樣式。eg:datecou.tostring(“d“);
格式化代碼及其含意:
d——以下列格式給出日月年:thursday,september 21,2001
g——09/22/2001 09:12:23
t——給出時間:01:23:22 時 分 秒
t——給出時分:01:23
d——mm/dd/yyyy
數據輸入
convert函數大全:
convert.toboolean();
convert.tobyte();
convert.tochar();
convert.todatetime();
convert.todecimal();
convert.todouble();
convert.toint16();
convert.toint32();
convert.toint64();
convert.tosbyte();
convert.tosingle();
convert.touint16();
convert.touint32();
convert.touint64();
七、常量
賦值:const int absolutezero=-273;
必須在類定義中聲明,而不能超脫類來定義。
八、結構化的數據類型
1、數組
聲明一個數組:string [] strsz;
為了生成一個已聲明的數組,必須進行初始化:strsz=new string[5];
聲明的同時進行初始化:string [] asp=new string[44];
數組的下標從0開始。
變量的賦值:asp[0]=“sdf“;
asp[2]=“sdf“;
……
或者:
string [] asp=new string[]{“asdf“,“asdfas“};
string [] asp=new string[2]{“asdf“,“asdfas“};
string [] asp={“asdf“,“asdfas“};
看實例:
<script runat="server" language="c#">
void page_load()
{
string[] strarraydetails = new string[3];
int intloop;
strarraydetails[0] = text1.text;
strarraydetails[1] = text2.text;
strarraydetails[2] = text3.text;
message1.text = strarraydetails[0];
message2.text = strarraydetails[1];
message3.text = strarraydetails[2];
}
</script>
<html>
<head>
<title>text box example</title>
</head>
<body>
<asp:label id="message1" runat="server" />
<br />
<asp:label id="message2" runat="server" />
<br />
<asp:label id="message3" runat="server" />
<br />
<form runat="server">
please enter your name:
<asp:textbox id="text1" runat="server" />
<br /><br />
please enter your address:
<asp:textbox id="text2" runat="server" rows=5 textmode="multiline" />
<br /><br />
please enter your chosen password:
<asp:textbox id="text3" runat="server" textmode="password" />
<br /><br />
<input type="submit">
</form>
</body>
</html>
多維數組:
string [,] strstr1=new string[3,2];
c#不限制數組的大小,只有內存來限制它,然而在實際使用過程中,當有超過三維以上的數組時,就需要考慮其它的解決方案,比如對象。
不規則數組:
string [] strnre=new string[2][];
string strnre[0]=new string[8];
string strnre[1]=new string[2];
2、結構
定義:struct nameabc{……};
3、枚舉
enum gender{male=0,female=1};