|
nbsp; '''' Desctructor code to free unmanaged resources MyBase.Finalize() End Sub End Class
class SuperHero { private int _powerLevel;
public SuperHero() { _powerLevel = 0; }
public SuperHero(int powerLevel) { this._powerLevel= powerLevel; }
~SuperHero() { // Destructor code to free unmanaged resources. // Implicitly creates a Finalize method } }
Objects
Dim hero As SuperHero = New SuperHero With hero .Name = "SpamMan" .PowerLevel = 3 End With
hero.Defend("Laura Jones") hero.Rest() '''' Calling Shared method '''' or SuperHero.Rest()
Dim hero2 As SuperHero = hero '''' Both refer to same object hero2.Name = "WormWoman" Console.WriteLine(hero.Name) '''' Prints WormWoman
hero = Nothing '''' Free the object
If hero Is Nothing Then _ hero = New SuperHero
Dim obj As Object = New SuperHero If TypeOf obj Is SuperHero Then _ Console.WriteLine("Is a SuperHero object.")
SuperHero hero = new SuperHero();
// No "With" construct hero.Name = "SpamMan"; hero.PowerLevel = 3;
hero.Defend("Laura Jones"); SuperHero.Rest(); // Calling static method
SuperHero hero2 = hero; // Both refer to same object hero2.Name = "WormWoman"; Console.WriteLine(hero.Name); // Prints WormWoman
hero = null ; // Free the object
if (hero == null) hero = new SuperHero();
Object obj = new SuperHero(); if (obj is SuperHero) Console.WriteLine("Is a SuperHero object.");
Structs
Structure StudentRecord Public name As String Public gpa As Single
Public Sub New(ByVal name As String, ByVal gpa As Single) Me.name = name Me.gpa = gpa End Sub End Structure
Dim stu As StudentRecord = New StudentRecord("Bob", 3.5) Dim stu2 As StudentRecord = stu
stu2.name = "Sue" Console.WriteLine(stu.name) '''' Prints Bob Console.WriteLine(stu2.name) '''' Prints Sue
struct StudentRecord { public string name; public float gpa;
public StudentRecord(string name, float gpa) { this.name = name; this.gpa = gpa; } }
StudentRecord stu = new StudentRecord("Bob", 3.5f); StudentRecord stu2 = stu;
stu2.name = "Sue"; Console.WriteLine(stu.name); // Prints Bob Console.WriteLine(stu2.name); // Prints Sue
Properties
Private _size As Integer
Public Property Size() As Integer Get Return _size End Get Set (ByVal Value As Integer) If Value < 0 Then _size = 0 Else _size = Value End If End Set End Property
foo.Size += 1
private int _size;
public int Size { get { return _size; } set { if (value < 0) _size = 0; else _size = value; } }
foo.Size++;
Delegates / Events
Delegate Sub MsgArrivedEventHandler(ByVal message As String)
Event MsgArrivedEvent As MsgArrivedEventHandler
'''' or to define an event which declares a delegate implicitly Event MsgArrivedEvent(ByVal message As String)
AddHandler MsgArrivedEvent, AddressOf My_MsgArrivedCallback '''' Won''''t throw an exception if obj is Nothing RaiseEvent MsgArrivedEvent("Test message") RemoveHandler MsgArrivedEvent, AddressOf My_MsgArrivedCallback
Imports System.Windows.Forms
Dim WithEvents MyButton As Button '''' WithEvents can''''t be used on local variable MyButton = New Button
Private Sub MyButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyButton.Click MessageBox.Show(Me, "Button was clicked", "Info", _ MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub
delegate void MsgArrivedEventHandler(string message);
event MsgArrivedEventHandler MsgArrivedEvent;
// Delegates must be used with events in C#
MsgArrivedEvent += new MsgArrivedEventHandler(My_MsgArrivedEventCallback); MsgArrivedEvent("Test message"); // Throws exception if obj is null MsgArrivedEvent -= new MsgArrivedEventHandler(My_MsgArrivedEventCallback);
using System.Windows.Forms;
Button MyButton = new Button(); MyButton.Click += new System.EventHandler(MyButton_Click);
private void MyButton_Click(object sender, System.EventArgs e) { MessageBox.Show(this, "Button was clicked", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); }
Console I/O
Special character constants vbCrLf, vbCr, vbLf, vbNewLine vbNullString vbTab vbBack vbFormFeed vbVerticalTab "" Chr(65) '''' Returns ''''A''''
Console.Write("What''''s your name? ") Dim name As String = Console.ReadLine() Console.Write("How old are you? ") Dim age As Integer = Val(Console.ReadLine()) Console.WriteLine("{0} is {1} years old.", name, age) '''' or Console.WriteLine(name & " is " & age & " years old.")
Dim c As Integer c = Console.Read() '''' Read single char Console.WriteLine(c) '''' Prints 65 if user enters "A"
Escape sequences \n, \r \t \\ \"
Convert.ToChar(65) // Returns ''''A'''' - equivalent to Chr(num) in VB // or (char) 65
Console.Write("What''''s your name? "); string name = Console.ReadLine(); Console.Write("How old are you? "); int age = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("{0} is {1} years old.", name, age); // or Console.WriteLine(name + " is " + age + " years old.");
int c = Console.Read(); // Read single char Console.WriteLine(c); // Prints 65 if user enters "A"
File I/O
Imports System.IO
Dim writer As StreamWriter = File.CreateText("c:\myfile.txt") writer.WriteLine("Out to file.") writer.Close()
Dim reader As StreamReader = File.OpenText("c:\myfile.txt") Dim line As String = reader.ReadLine() While Not line Is Nothing Console.WriteLine("line=" & line) line = reader.ReadLine() End While reader.Close()
Dim str As String = "Text data" Dim num As Integer = 123 Dim binWriter As New BinaryWriter (File.OpenWrite("c:\myfile.dat")) binWriter.Write(str) binWriter.Write(num) binWriter.Close()
Dim binReader As New BinaryReader (File.OpenRead("c:\myfile.dat")) str = binReader.ReadString() num = binReader.ReadInt32() binReader.Close()
using System.IO;
StreamWriter writer = File.CreateText("c:\\myfile.txt"); writer.WriteLine("Out to file."); writer.Close();
StreamReader reader = File.OpenText("c:\\myfile.txt"); string line = reader.ReadLine(); while (line != null) { Console.WriteLine(line); line = reader.ReadLine(); } reader.Close();
string str = "Text data"; int num = 123; BinaryWriter binWriter = new BinaryWriter(File.OpenWrite("c:\\myfile.dat")); binWriter.Write(str); binWriter.Write(num); binWriter.Close();
上一页 [1] [2] [3] [4] 下一页 [C语言系列]C# 过滤html,js,css代码 正则表达式 [C语言系列]C# DataGridView显示行号的两种方法 [C语言系列]C# WinForm 中Label自动换行 解决方法 [C语言系列]C# 线程调用主线程中的控件 [电脑应用]c# winform 打包部署 自定义界面 或设置开机启动 [C语言系列]C# 和 Linux 时间戳转换 [C语言系列]C#实现 WebBrowser中新窗口打开链接用默认或者指定… [C语言系列]C#全角和半角转换 [C语言系列]c#WebBrowser查找并选择文本 [C语言系列]C#中实现WebBrowser控件的HTML源代码读写
|