快上网建站品牌

13518219792
  • 首页
  • 关于我们
    • 如何选择
    • 选择理由
  • 案例作品
    • 网站建设
    • 优化推广
    • 微信开发
    • 电商托管
  • 服务项目
    • 网站建设
    • 移动端/APP
    • 微信/小程序
    • 技术支持
    • 其它服务
  • 建站知识
    • 成都网站建设
    • 成都做网站
    • 成都网站设计
  • 网站售后
    • 成都网站运营
    • 成都网站维护
    • 成都网站推广
  • 客服中心
  • 全国分站

ASP.NET基础语法概览

本文提供的的代码段帮助你理解ASP.NET的基础语法。

成都创新互联公司主营墨江网站建设的网络公司,主营网站建设方案,成都app开发,墨江h5微信小程序开发搭建,墨江网站营销推广欢迎墨江等地区企业咨询

变量声明

 
 
 
  1. int x; 
  2. String s; 
  3. String s1, s2; 
  4. Object o; 
  5. Object obj = new Object(); 
  6. public String name; 

语句

 
 
 
  1. Response.Write("foo"); 

注释

 
 
 
  1. // 这是单行注释 
  2. /* 
  3. 这是 
  4. 多 
  5. 行 
  6. 注释*/ 

访问索引属性

 
 
 
  1. String s = Request.QueryString["Name"]; 
  2. String value = Request.Cookies["key"]; 

声明索引属性

 
 
 
  1. // Default Indexed Property 
  2. public String this[String name] { 
  3. get { 
  4. return (String) lookuptable[name]; 
  5. } 
  6. } 

声明简单属性

 
 
 
  1. public String name { 
  2. get { 
  3. ... 
  4. return ...; 
  5. } 
  6. set { 
  7. ... = value; 
  8. } 
  9. } 

声明和使用枚举

 
 
 
  1. // Declare the Enumeration 
  2. public enum MessageSize { 
  3. Small = 0, 
  4. Medium = 1, 
  5. Large = 2 
  6. } 
  7. // Create a Field or Property 
  8. public MessageSize msgsize; 
  9. // Assign to the property using the Enumeration values 
  10. msgsize = Small; 

遍历集合

 
 
 
  1. foreach ( String s in coll ) { 
  2. ... 
  3. } 

声明和使用方法

 
 
 
  1. // Declare a void return function 
  2. void voidfunction() { 
  3. ... 
  4. } 
  5. // Declare a function that returns a value 
  6. String stringfunction() { 
  7. ... 
  8. return (String) val; 
  9. } 
  10. // Declare a function that takes and returns values 
  11. String parmfunction(String a, String b) { 
  12. ... 
  13. return (String) (a + b); 
  14. } 
  15. // Use the Functions 
  16. voidfunction(); 
  17. String s1 = stringfunction(); 
  18. String s2 = parmfunction("Hello", "World!"); 

定制属性

 
 
 
  1. // Stand-alone attribute 
  2. [STAThread] 
  3. // Attribute with parameters 
  4. [DllImport("ADVAPI32.DLL")] 
  5. // Attribute with named parameters 
  6. [DllImport("KERNEL32.DLL", CharSet=CharSet.Auto)] 

数组

 
 
 
  1. String[] a = new String[3];
  2. a[0] = "1";
  3. a[1] = "2";
  4. a[2] = "3";
  5. String[][] a = new String[3][3];
  6. a[0][0] = "1";
  7. a[1][0] = "2";
  8. a[2][0] = "3";

初始化

 
 
 
  1. String s = "Hello World";
  2. int i = 1;
  3. double[] a = { 3.00, 4.00, 5.00 };

ASP.NET基础语法:语句

If 语句

 
 
 
  1. if (Request.QueryString != null) {
  2. ...
  3. }

Case 语句

 
 
 
  1. switch (FirstName) {
  2. case "John" :
  3. ...
  4. break;
  5. case "Paul" :
  6. ...
  7. break;
  8. case "Ringo" :
  9. ...
  10. break;
  11. default:
  12. ...
  13. break;
  14. }

For 循环

 
 
 
  1. for (int i=0; i<3; i++)
  2. a(i) = "test";

While 循环

 
 
 
  1. int i = 0;
  2. while (i<3) {
  3. Console.WriteLine(i.ToString());
  4. i += 1;
  5. }

异常处理

 
 
 
  1. try {
  2. // Code that throws exceptions
  3. } catch(OverflowException e) {
  4. // Catch a specific exception
  5. } catch(Exception e) {
  6. // Catch the generic exceptions
  7. } finally {
  8. // Execute some cleanup code
  9. }

字符串连接

 
 
 
  1. // Using Strings
  2. String s1;
  3. String s2 = "hello";
  4. s2 += " world";
  5. s1 = s2 + " !!!";
  6. // Using StringBuilder class for performance
  7. StringBuilder s3 = new StringBuilder();
  8. s3.Append("hello");
  9. s3.Append(" world");
  10. s3.Append(" !!!");

事件处理委派

 
 
 
  1. void MyButton_Click(Object sender,
  2. EventArgs E) {
  3. ...
  4. }

声明事件

 
 
 
  1. // Create a public event
  2. public event EventHandler MyEvent;
  3. // Create a method for firing the event
  4. protected void OnMyEvent(EventArgs e) {
  5. MyEvent(this, e);
  6. }

向事件增加或移除事件处理

 
 
 
  1. Control.Change += new EventHandler(this.ChangeEventHandler);
  2. Control.Change -= new EventHandler(this.ChangeEventHandler);

构造

 
 
 
  1. MyObject obj = (MyObject)Session["Some Value"];
  2. IMyObject iObj = obj;

转换

 
 
 
  1. int i = 3;
  2. String s = i.ToString();
  3. double d = Double.Parse(s);

带有继承的类定义

 
 
 
  1. using System;
  2. namespace MySpace {
  3. public class Foo : Bar {
  4. int x;
  5. public Foo() { x = 4; }
  6. public void Add(int x) { this.x += x; }
  7. override public int GetNum() { return x; }
  8. }
  9. }
  10. // csc /out:librarycs.dll /t:library
  11. // library.cs

实现接口

 
 
 
  1. public class MyClass : IEnumerable {
  2. ...
  3. IEnumerator IEnumerable.GetEnumerator() {
  4. ...
  5. }
  6. }

带有Main方法的类定义

 
 
 
  1. using System;
  2. public class ConsoleCS {
  3. public ConsoleCS() {
  4. Console.WriteLine("Object Created");
  5. }
  6. public static void Main (String[] args) {
  7. Console.WriteLine("Hello World");
  8. ConsoleCS ccs = new ConsoleCS();
  9. }
  10. }
  11. // csc /out:consolecs.exe /t:exe console.cs

标准模板

 
 
 
  1. using System;
  2. public class Module {
  3. public static void Main (String[] args) {
  4. Console.WriteLine("Hello World");
  5. }
  6. }
  7. // csc /out:consolecs.exe /t:exe console.cs

以上就介绍了ASP.NET基础语法。


当前文章:ASP.NET基础语法概览
标题来源:http://gydahua.com/article/ccspshj.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流

其他资讯

  • 美国云服务器怎样挑选不踩雷
  • 域服务器密码忘记?域服务器密码怎么修改
  • C#动态创建数组详细实现过程解析
  • 「OA管理系统数据库设计」详解——技术实现全流程 (oa管理系统数据库设计)
  • 试用WindowsEmbedded6.0R3开发手记

行业动态

企业网站建设的重要性!

现在虽然是移动互联网时代,但企业网站依然重要,包含PC站点,移动站。可以说企业网站关系企业的未来发展和前途,尤其对中小企业更是如此,一些中小企业老板,对自己的名片很在乎,因为这是个门面。...

服务项目

  • 网站建设

    查看详情
  • 移动端/APP

    查看详情
  • 微信/小程序

    查看详情
  • 技术支持

    查看详情
  • 其它服务

    查看详情
  • 更多服务项目

    用我们的专业和诚信赢得您的信赖,从PC到移动互联网均有您想要的服务!

    获取更多

联系吧 在百度地图上找到我们

电话:13518219792

如遇占线或暂未接听请拨:136xxx98888

业务咨询 技术咨询 售后服务
网站设计
宜宾网站设计
成都网站设计
LED网站设计方案
温江网站设计
网站制作
定制网站制作
重庆网站制作
移动手机网站制作
绵阳网站制作
联系我们
电话:13518219792
邮箱:631063699@qq.com
地址:成都青羊区锦天国际1002号
网址:www.gydahua.com
网站建设
成都网站建设公司
成都营销网站建设
网站建设方案
品牌网站建设

微信二维码

  • 友情链接
  • 大邑珉田数据中心
  • 雅安上柴发电机组
  • 香港云空间
  • 商标注册
  • 成都网站建设
  • 德阳网站建设公司
  • 成都个体注册
  • pzhyuzhou.com
  • 免备案虚拟主机
  • 江安网站建设

Copyright © 2002-2023 www.gydahua.com 快上网建站品牌 QQ:244261566 版权所有 备案号:蜀ICP备19037934号

  • 在线咨询
  • 13518219792
  • 微信二维码

  • 移动版官网