C#3.0 learning
来源:互联网 发布:宏观对冲 知乎 编辑:程序博客网 时间:2024/06/06 01:39
Difference between C++ and C sharp
- array differing from array in c++, is the collection of objects. Normally array objects are used by followed by initialization followed declaration a
- 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}type [][] anJaggedVariable;
int [][] onMember; - 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.
- foreach the substitute for the for or while statements , foreach( type identity in expression)
- 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(5, 6, 7, 8);
int[] explicitArray = new int[5] ...{ 1, 2, 3, 4, 5 };
t.DisplayVals(explicitArray);
}
public void DisplayVals(params int[] intVals)
...{
foreach (int i in intVals)
Console.WriteLine("DisplayVals {0}", i);
}
}
}
- multidimesional Arrays Rectangular Array is multidimensional Array with fixed lengths for each dimension.
- 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();
.....
}
}
}
- 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 .
- struct value type unlike struct in C++ but can implement multiple interfaces.
- object the root class ,inherited by all class in C#,is abstract class
- 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
} - 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()
...{
};
}
} - inheritance there is no protect and private inheritance, and multiple inheritance only for interface
- 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.
}
} - 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);
}
}
}
- class defination don't end with colon class NewClass
{
} - C# not have point ' concept. this isn't a pointer but a reference public void someMethod(int hour)
...{
this->hour=hour;
} - 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(); - 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 - 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.
- 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
...{
bool is_disposed = false;
protected virtual void Dispose(bool disposing)
...{
if( !is_disposed)
...{
if( disposing)
...{
Console.WriteLine(
"not in destructor, ok to refernce other objects");
}
Console.WriteLine("Disposing.......");
}
this.is_disposed = true;
}
public void Dispose()
...{
Dispose(true);
GC.SuppressFinalize(this);
}
~Testing()
...{
Dispose(this);
Console.WritLine("In destruction");
}
}
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
}
}
- C#3.0 learning
- Learning C# Attribute
- Visual Studio C# Learning Notes
- the step of learning C#
- Learning hard C#学习笔记
- C# learning之Class与Struct
- 《Learning hard C#学习笔记》阅读总结
- Learning
- learning
- learning
- learning
- learning
- learning
- learning
- 记《learning hard C#学习笔记》 书中一个错误
- learning hard c#学习笔记总结知识点(1)
- Tips for learning ActionScript 3.0
- Learning XNA 3.0翻译连载
- 文思培训第一天
- 【CBM666 的递归文件搜索】
- 我在百度的空间
- 常用的简单 CSS布局
- Qtopia Core 4.3(QT/E)交叉编译安装全过程[转]
- C#3.0 learning
- COM+ 无法与 Microsoft 分布式事务协调程序交谈
- 变量的初始化规则
- html学习之三:列表&超链接
- [C/C++] 可变参数表(Variable Arguments)
- adodb学习
- 手机J2ME程序开发(一)
- [资料]I386平台的内存布局
- PLC在自来水净化加药过程中的应用