首页 > 应用开发 > IT综合 > 使用C#轻松编写.Net组件
| using System; namespace ComponentCS { public class StringComponent { private string[] StringsSet; public int StringLength { get { return StringsSet.Length; } } public void Modify(int index,string value) { if ((index < 0) || (index >= StringsSet.Length)) { throw new IndexOutOfRangeException(); } else { StringsSet[index]=value; OnModify(); } } public StringComponent() { StringsSet = new string[] { "C# String 0", "C# String 1", "C# String 2", "C# String 3" }; } public string GetString(int index) { if ((index < 0) || (index >= StringsSet.Length)) { throw new IndexOutOfRangeException(); } return StringsSet[index]; } } } |
| namespace CompCS |
| namespace NestIt{ namespace NestedNameSpace { class myClass { public static void DoSth() { ... } } }} |
| ??NestIt.NestedNameSpace.myClass.DoSth(); |
| ??public class StringComponent |
| ??private string[] StringsSet; |
| public int StringLength{ get { return StringsSet.Length; }} |
| ??public StringComponent() |
| public string GetString(int index) { … return StringsSet[index];} |
| ??throw new IndexOutOfRangeException(); |
| public class MyApplicationException : ApplicationException{ public string AMsg; public MyApplicatonException(string strMsg) { AMsg=strMsg; } } |
| public string GetString(int index) { try { if ((index < 0) || (index >= StringsSet.Length)) { throw new MyApplicationException("参数超出范围"); } } catch(MyApplicationException mErr) { Console.WriteLine(mErr.AMsg); } catch(Exception Err) { Console.WriteLine(Err.Message); } return StringsSet[index]; } |
| ??public event EventHandler Modified; |
| public void Modify(int index,string value) { if ((index < 0) || (index >= StringsSet.Length)) { throw new IndexOutOfRangeException(); } else { StringsSet[index]=value; OnModify(); } } private void OnModify() { EventArgs e=new EventArgs(); if(!(Modified==null)) Modified(this,e); } |
| private void DoIt(){ StringComponent mysc=new StringComponent(); mysc.Modified+=new EventHandler(Called); mysc.Modify(2,"another string");}public void Called(object o,EventArgs e){ Console.WriteLine("Changed");} |
| ??mysc.Modified+=new EventHandler(Called); |
| using System; namespace ComponentCS { public class StringComponent { private string[] StringsSet; public event EventHandler Modified; public int StringLength { get { return StringsSet.Length; } } public void Modify(int index,string value) { if ((index < 0) || (index >= StringsSet.Length)) { throw new IndexOutOfRangeException(); } else { StringsSet[index]=value; OnModify(); } } private void OnModify() { EventArgs e=new EventArgs(); if(!(Modified==null)) Modified(this,e); } public StringComponent() { StringsSet = new string[] { "C# String 0", "C# String 1", "C# String 2", "C# String 3" }; } public string GetString(int index) { if ((index < 0) || (index >= StringsSet.Length)) { throw new IndexOutOfRangeException(); } return StringsSet[index]; } } } |
| ??csc.exe /t:library /debug+ /out:myCom.dll example.cs |
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/8781179/viewspace-921326/ ,如需转载,请注明出处,否则将追究法律责任。