Asp.Netの条件とループ文の例(If,Swich,While,For,Goto,foreach,Lockは直接実行可能)

8039 ワード


        #region =========================================================== \\ ★ 【    】

            Response.Write(" <br>----------【    】 ------------- <br>");

            //      :  ?  :

            int isyesno=1;
            String yesno=isyesno==1?" ":"  ";
            Response.Write(yesno);
                  
        #endregion


        #region =========================================================== \\ ★ 【If     】


            Response.Write("<br>----------【If     】 ------------- <br>");

           //  if ()
           //  {}
           //  Else
           //  {}

            int i=3;
            if (i == 1)
            {
                Response.Write("I=1");
            }
            else if (i == 2)
            {
                Response.Write("I=2");
            }
            else
            {
                Response.Write("I=3");
            }

                  
        #endregion


        #region =========================================================== \\ ★ 【switch     】


                Response.Write("<br>----------【switch     】 ------------- <br>");

                // switch                  :sbyte、byte、short、ushort、int、uint、long、ulong、char、string
                //            ,        。break      。Default  

                int ii = 1;
                switch (ii)
                {
                    case 1:
                        Response.Write("ii=1");
                        break;
                    case 2:
                        Response.Write("ii=2");
                        break;
                    default:
                        Response.Write("ii=3");
                        break;
                }

      #endregion


       #region =========================================================== \\ ★ 【while      +1】


                Response.Write("<br>----------【while     】 ------------- <br>");

                // break        continue     ,   Loop , return         ,     

                int v = 0;
                int s=0;
                while (v<100)
                {
                    v=v+1;
                    if (v == 5 || v == 45)
                    {
                        continue;
                    }
                    s = s+ v;
                    if (v==101)
                    { 
                        break;
                    }

                }
                Response.Write("1+.."+v.ToString()+"="+s.ToString());

      #endregion


          #region =========================================================== \\ ★ 【For     】


                Response.Write("<br>----------【For     】 ------------- <br>");

                //   ,   、          。       ,           ,       (break  goto)    。 
                int bb = 0;
                int b = 0;
                for (b = 0; b <= 100; b++) 
                {
                    if (b > 100)
                    {
                        break; //        
                    }
                    bb = bb + b;
                }

                Response.Write("1+.." + b.ToString() + "=" + bb.ToString());

       #endregion

         #region =========================================================== \\ ★ 【foreach     】

             Response.Write("<br>----------【foreach     】 ------------- <br>");

             string[] m_name = new string[5] { " ", " ", " ", " ", " " };
             foreach (string u in m_name)
             {
                 Response.Write(u+"<Br>");
             }
            
            //                Response.Write("1+.." + v.ToString() + "=" + s.ToString());

                #endregion


         #region =========================================================== \\ ★ 【Try   】

             Response.Write("<br>----------【Try   】 ------------- <br>");

             string f = "ok";
             int ff = 0;
             string c;
             int abc;
             try
             {
                 abc = int.Parse(Request.Form["abc"]); 
                 c = f + ff + 5;
                 Response.Write(c);
             }
             catch 
             {
                 Response.Write("  Error");
             }
            //                Response.Write("1+.." + v.ToString() + "=" + s.ToString());

         #endregion

         #region =========================================================== \\ ★ 【lock   】

             Response.Write("<br>----------【lock   】 ------------- <br>");

/*
            static void Main() { 
                 A a = ...; 
                 lock(a) { 
                     a.P = a.P + 1; 
                 } 
             } 

             using statements 
              static void Main() { 
                 using (Resource r = new Resource()) { 
                     r.F(); 
                 } 
             }

                             Response.Write("1+.." + v.ToString() + "=" + s.ToString());

            asp.net    http   HttpApplication      HttpApplication       http  , Httpmodule   http handle  .  asp.net            ,            httpcontext    ,                           http   .   asp.net     lock(this),  lock(       ),  http      lock          ,    lock  .             ,  asp.net                ,    lock(this),  lock(       )  this,             ,           .  
                 :  
             protected void Button1_Click(object sender, EventArgs e)  
                {  
                    lock (this)  
                    {  
                        for (int i = 0; i < 10; i++)  
                        {  
                            Thread.Sleep(1000);  
                            Response.Write(DateTime.Now.ToString());  
                        }  
                    }  
                }  
                    ,      button1,    2           1      ,         1      2   ,   lock  .  
                 ,         http        :  
            private static object o=new object()  
            protected void Button1_Click(object sender, EventArgs e)  
                {  
                    lock (o)  
                    {  
                        for (int i = 0; i < 10; i++)  
                        {  
                            Thread.Sleep(1000);  
                            Response.Write(DateTime.Now.ToString());  
                        }  
                    }  
                }  
              o static ,   asp.net      ,          .  
               sleep      , wait     .  
                         (   asp.net  )           ,    asp.net         .lock              . 

                        */

         #endregion


         #region =========================================================== \\ ★ 【Goto   】

             Response.Write("<br>----------【Goto   】 ------------- <br>");

                //       goto    
             string args="";
                if (args.Length == 0) 
                {
                    goto done;
                }
                    Response.Write(args);
                done:
                    Response.Write("    !");


                    int si = 0;
                    while (si < 6)
                    {
                        Response.Write("<Br>  si  " + si.ToString().Trim() + "<br>");
                        if (si == 3)
                            goto Complete; //  i  5,   Complete       while  ;
                        si++;
                    }
                    Response.Write("<br>     !");
                Complete:
                    Response.Write("<br>          !");
         #endregion
声明:一部のコードは手間が省けるため、インターネットから抜粋したものです.