C#3.0 learning

来源:互联网 发布:宏观对冲 知乎 编辑:程序博客网 时间:2024/06/06 01:39

Difference between C++ and  C sharp 


  1. array differing  from array in c++, is the collection of objects. Normally array objects are used by followed by initialization followed declaration a
    1. multidimesional Arrays Rectangular Array  is  multidimensional Array  with fixed lengths for each dimension.
      type [ ,] array-name

      int  [2,3] anRectangularVariabe = new int[2,3]{1,2,3,4,5,6}
       jagged array  is multidimesional array with each dimension length ,which set aside for the defination
      type [][] anJaggedVariable;
      int [][]  onMember;
    2. as for array being an object, array  is reference ,created on the heap,nevertheless the location of the members of the array is up to the kind of the type of them, the memory of value type member allocated on the heap, the memory of reference type allocated on the stack.
    3. foreach  the  substitute  for the for  or  while statements , foreach(  type identity in expression)
    4. the params keyword  is used by defining the varialbe member.
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;

      namespace UsingParams
      {

          
      public class Tester
          
      {
              
      static void Main(string[] args)
              
      {
                  Tester t 
      = new Tester();
                  t.DisplayVals(
      5678);
                  
      int[] explicitArray = new int[512345 };
                  t.DisplayVals(explicitArray);
                  
              }


              
      public void DisplayVals(params int[] intVals)
              
      {
                  
      foreach (int i in intVals)
                      Console.WriteLine(
      "DisplayVals {0}", i);
              }

          }

      }

  2. explicitly implementing,generaly speaking, an implementing class implicitly  implements  member methods if be-inherited interfaces have any common names, or explicitly do, with side influence that the derived class can't define the member methods as virtual and abstract type.
    namespace ExplicitlyInterface
    {
            
    public interface TheFistInterface()
             
    {

    void read();
                  
    void write();
             }

                  

      
            
    public interface TheSecondInterface()
            
    {
                
    void read();
                
    void input();
             }


            
    public class SomeClass :TheFirstInterface ,TheSecondInterface
            
    {
                  
    public void write()
                  

                       
    //do some commands
                  }

                  
    public void TheFirstInterface.read()
                  
    {
                       
    //do some commands
                  }

                  
    public void input() 
                  
    {
                        .........
                  }

                 
    public void read()
                  

                       ......
                  }

             }

          
    class program
             
    {
                
    public static Main()
               
    {
                     SomeClass oneClass 
    = new SomClass();
                 .....
                }

             }

    }

               
             
  3. interface  mix-in for  class , can't be instantiated only through the implementing class,and there is not any constructor and destructor, concluding methods, properties ,events and the indexers,most of all ,the implementing  class must implement all the implemented interfaces' all  things  or  generate  errors . interface also can be inherited though multiple and single ,and be combined by additional members .
  4. struct  value type unlike struct in C++ but can implement multiple interfaces.
  5. object  the root class ,inherited by all class in C#,is abstract class
  6. abstract class  an abstract class which doesn't have any instance ,implicitly declared by its abstract member method,but if there are more abstract member methods ,explicitly marking the definition with the keyword abstract .sealed class is defined by preceding the class with  keyword sealed,which can't be inherited.
    namespace AbstractClass
    {
            
    public abstract class OneClass
             

                  
    public abstract TheFirstMethodO;
                  
    public abstract TheSecondMethod();
              }

            
    public class AnotherClass
             
    {
                
    public abstract OneClass()(;
              }
     // abstract class
    }
  7. polymorphism To create a method supporting the polymorphism ,must precede the method with keyword virtual in base class ,and then override the method in derived class or not,for enhancing the loosens of the design , you can add keyword new before the virtual member method
     namespace PolymorphismExample
    {
              
    class button
              
    {
                     
    public virtual void SomeMethod()
                      
    {
                        
    //do some thing
                       }
    ;
                 ............
                }

                
    public class ListButton: button
                
    {
                      
    public override void SomeMethod()
                      
    {
                      }
    ;
                  }

                 
    public class TreeButton: button
                 
    {
                      
    public new void SomeMethod()
                      
    {
                       }
    ;
                   }

    }
  8. inheritance  there is no protect and private inheritance, and multiple inheritance only for interface
  9. readonly a modifier that indicate the fields only are assigned in the contructor or in declaration
    class Age
       
    {
                  
    readonly int   year;
                  Age( 
    int year )
                   
    {
                             
    this-> year=year;
                    }

                    
    void ChangYear()
                    
    {
                       year
    =1967// will not compile.
                    }

    }
  10. properties which allow clients to directly access the private members through get{} and set()
    namespace UsingAPropety
    {
       
    public class Time
         
    {
                
    private int year;
                
    private int month;
                
    private int date;
                
    private int hour;
                
    private int minute;
                
    private int second;
              
                
    public void DisplayCurrenTime()
                
    {
                   System.Console.WriteLine(
    "Time :{0}/{1}/{2} {3}:{4}:{5}",month ,date ,year,hour,minute,second);
                 }

                
                 publicTime( System.DateTime dt)
                 
    {
                     year
    =dt.year;
                     month
    =dt.month;
                     date
    =dt.date;
                     hour
    =dt.hour;
                     minute
    =dt.minute;
                      second
    =dt.second;
                   }


                   
    public int Hour
                   
    {
                          
    get
                          
    {
                                  
    return  hour;
                           }

                           
    set
                            
    {
                                hour
    =value;
                            }

                     }

              }


              
    public class Tester
               
    {
                   
    static void Main()
                   
    {
                    System.DateTime currentTime 
    = System.Date.Now;
                    Time t 
    = new Time( currentTime );
                     t.DisplayCurrentTime();
                     
                      
    int theHour =  t.hour;
                      System.Console.WriteLine(
    "Retrieved the hour: {0} ",theHour);
                      theHour
    ++;
                      t.Hour 
    =  theHour;
                      System.Console.WriteLine(
    "Upated  the hour: {0} ",theHour);
                    
                            
                        }

                   }

    }

           
     
  11. class defination don't end with colon
    class NewClass
       {
       }
  12. C# not have point ' concept. this isn't a pointer  but a reference
    public void someMethod(int hour)
    {
         
    this->hour=hour;
    }
  13. static member      static member doesn't belong to the objects but the part of the class,in C#, It'illegal to access statci methods and member variables throught object and refernce.
    public class button
    {
        
    public static void someMethod
           
    {
              .............
           }


    }

       
    button updateButton;


    you can
        button
    ->someMethod();
    rather than
       updateButton
    ->someMethod();
  14. as for if ,while ,and so on    only the bool type value must be passed on  ,or cause complier eror.
    if( variable =3)   // c c++ is ok but c#
    if(varible==3)     // c# is ok
  15. destroying objects   C# adopt the methanism of garbage collection. destruction is called after the ending of the cope,but rather when it is garbage-collected,known as the nondeteministic finalation.
  16. C# destructor  objects is automatically garbage-collected,if intand destruction of the objects is in need ,first should overide the Dispose() ,and then inform the garbage collector don't call the destructor of the objects thought the static method GC.SupressFinalize();
    using System;
    class Testing:IDisposable
    ...

using statement    it is the best option that creating an object  in the using statement block is auomatically followed by destruction of the objects after the ending of the lifescope.       

namespze usingStatement
{
   
class Tester
    

        
public static void Main()
          
{
            
using ( Font theFont = new Font("Arial",10.of))
             
{
                 
//use theFont
             }
 // compiler will call Dispose on theFont
            Font anotherFont = Font("Courier",12.of);
             
using ( anthorFont)
               
{
                    
//use anotherFont
                }
 a fraught thoughts
               }

    }
原创粉丝点击