//z 2012-3-7 13:30:51 PM IS2120@CSDN
|
|||||||||||
Imports System Namespace Hello Class HelloWorld Overloads Shared Sub Main(ByVal args() As String) Dim name As String = "VB.NET" 'See if an argument was passedfrom the command line@H_301_33@ If args.Length = 1 Then name = args(0) Console.WriteLine("Hello," & name & "!") End Sub End Class End Namespace |
using System; namespace Hello { public class HelloWorld { public static void Main(string[] args) { string name = "C#"; // See if an argument was passed from the command line@H_301_33@ if (args.Length == 1) name = args[0]; Console.WriteLine("Hello," + name + "!"); } } } |
||||||||||
|
|||||||||||
' Single line only@H_301_33@ REMSingle line only@H_301_33@ ''' <summary>XML comments</summary>@H_301_33@ |
// Single line |
||||||||||
|
|||||||||||
Value Types@H_301_33@ Reference Types@H_301_33@ Initializing@H_301_33@ Nullable Types@H_301_33@ Implicitly Typed Local Variables@H_301_33@ Type Information@H_301_33@ Type Conversion / Casting@H_301_33@ |
Value Types@H_301_33@ Reference Types@H_301_33@ Initializing@H_301_33@ Nullable Types@H_301_33@ Implicitly Typed Local Variables@H_301_33@ Type Information@H_301_33@ Type Conversion / Casting@H_301_33@ |
||||||||||
|
|||||||||||
ConstMAX_STUDENTS As Integer = 25 ' Can set to a const orvar; may be initialized in a constructor@H_301_33@ |
const int MAX_STUDENTS = 25; // Can set to a const or var; may be initialized in a constructor@H_301_33@ |
||||||||||
|
|||||||||||
Enum Action Start [Stop]' Stopis a reserved word@H_301_33@ Rewind Forward End Enum EnumStatus Flunk = 50 Pass = 70 Excel = 90 End Enum Dim a As Action = Action.Stop If a <> Action.Start Then _ Console.WriteLine(a.ToString & " is " & a) ' Prints "Stop is 1"@H_301_33@ Console.WriteLine(Status.Pass) ' Prints 70@H_301_33@ Console.WriteLine(Status.Pass.ToString()) ' Prints Pass@H_301_33@@H_301_33@ |
enum Action {Start,Stop,Rewind,Forward}; enum Status {Flunk = 50,Pass = 70,Excel = 90}; Action a = Action.Stop; if (a != Action.Start) Console.WriteLine(a + " is " + (int) a);// Prints "Stop is 1"@H_301_33@ Console.WriteLine((int) Status.Pass); // Prints 70@H_301_33@ Console.WriteLine(Status.Pass); // Prints Pass@H_301_33@ |
||||||||||
|
|||||||||||
Comparison@H_301_33@ Arithmetic@H_301_33@ Assignment@H_301_33@ Bitwise@H_301_33@ Logical@H_301_33@ Note: AndAlso and OrElse perform short-circuit logical evaluations String Concatenation@H_301_33@ |
Comparison@H_301_33@ Arithmetic@H_301_33@ Assignment@H_301_33@ Bitwise@H_301_33@ Logical@H_301_33@ Note: && and||perform short-circuit logical evaluations String Concatenation@H_301_33@ |
||||||||||
|
|||||||||||
' Null-coalescing operator if called with 2 arguments@H_301_33@ ' Ternary/Conditional operator (IIf evaluates 2nd and 3rd expressions)@H_301_33@ ' One line doesn't require "End If"@H_301_33@ ' Use : to put two commands on same line@H_301_33@ ' Preferred@H_301_33@ ' Use _ to break up long single line or use implicit line break@H_301_33@ '@H_301_33@If x > 5 Then Select Case color ' Must be a primitive data type@H_301_33@ |
// Null-coalescing operator@H_301_33@ // Ternary/Conditional operator@H_301_33@ if (age < 20) // Multiple statements must be enclosed in {}@H_301_33@ No need for _ or : since ; is used to terminate each statement.@H_301_33@
|
||||||||||
|
|||||||||||
' Array or collection looping@H_301_33@ ' Breaking out of loops@H_301_33@ ' Continue to next iteration@H_301_33@ |
Pre-test Loops:@H_301_33@ // no "until" keyword@H_301_33@
// Array or collection looping@H_301_33@
// Continue to next iteration@H_301_33@ |
||||||||||
|
|||||||||||
Dim nums() As Integer = {1,3} Dim twoD(rows-1,cols-1) As Single |
int[] nums = {1,3}; float[,] twoD = new float[rows,cols]; int[][] jagged = new int[3][] { |
||||||||||
|
|||||||||||
' Pass by value (in,default),reference (in/out),andreference (out)@H_301_33@ Dim a = 1,b = 1,c As Integer ' cset to zero by default@H_301_33@ ' Accept variable number of arguments@H_301_33@ ' Optional parameters must belisted lastand must have a default value@H_301_33@ |
// Pass by value (in,andreference (out)@H_301_33@ void TestFunc(int x,ref int y,out int z) { x++; y++; z = 5; } int a = 1,c; // c doesn't need initializing@H_301_33@ // Accept variable number of arguments@H_301_33@ int total = Sum(4,1); // returns 10@H_301_33@ /* C# 4.0 supports optional parameters. PrevIoUs versions required function overloading. */@H_301_33@ SayHello("Strangelove","Dr."); |
||||||||||
|
|||||||||||
Special character constants (all also accessible from ControlChars class)@H_301_33@ ' String concatenation (use & or +)@H_301_33@ ' Chars@H_301_33@ ' No string literal operator@H_301_33@ ' String comparison@H_301_33@ ' String matching with Like - Regex is more powerful@H_301_33@ ' Substring@H_301_33@ ' Replacement@H_301_33@ ' Split@H_301_33@ ' Date to string@H_301_33@ ' Integer to String@H_301_33@ ' String to Integer@H_301_33@ ' Mutable string |
Escape sequences@H_301_33@ // String concatenation@H_301_33@ // Chars@H_301_33@ // String literal@H_301_33@ // String comparison@H_301_33@ // String matching - No Like equivalent,use Regex@H_301_33@
// Replacement@H_301_33@ // Split@H_301_33@ // Date to string@H_301_33@ // int to string@H_301_33@ int x = Convert.ToInt32("-5"); // x is -5@H_301_33@ // Mutable string@H_301_33@ |
||||||||||
|
|||||||||||
Imports System.Text.RegularExpressions ' Match a string pattern@H_301_33@ ' Find and remember all matching patterns@H_301_33@ ' Remeber multiple parts of matched pattern@H_301_33@ ' Replace all occurrances of a pattern@H_301_33@ ' Replace matched patterns@H_301_33@ ' Split a string based on a pattern@H_301_33@ |
using System.Text.RegularExpressions; // Match a string pattern@H_301_33@
// Replace all occurrances of a pattern@H_301_33@ // Replace matched patterns@H_301_33@ // Split a string based on a pattern@H_301_33@ |
||||||||||
|
|||||||||||
' Throw an exception@H_301_33@ ' Catch an exception@H_301_33@ ' Deprecated unstructured error handling@H_301_33@ |
// Throw an exception@H_301_33@ // Catch an exception@H_301_33@ |
||||||||||
|
|||||||||||
Namespace Harding.Compsci.Graphics ' or@H_301_33@ Namespace Harding Imports Harding.Compsci.Graphics |
namespace Harding.Compsci.Graphics { // or@H_301_33@ namespace Harding { using Harding.Compsci.Graphics; |
||||||||||
|
|||||||||||
Access Modifiers@H_301_33@ Class Modifiers@H_301_33@ Method Modifiers@H_301_33@ ' All members are Shared@H_301_33@ ' Partial classes@H_301_33@ ' Inheritance@H_301_33@ ' Interface definition@H_301_33@ ' Extending an interface@H_301_33@ ' Interface implementation@H_301_33@ |
Access Modifiers@H_301_33@ Class Modifiers@H_301_33@ Method Modifiers@H_301_33@ No Module equivalent - just use static class@H_301_33@ // Partial classes@H_301_33@ // Inheritance@H_301_33@
// Extending an interface@H_301_33@
|
||||||||||
|
|||||||||||
Class SuperHero Inherits Person Private powerLevel As Integer Private name As String ' Default constructor@H_301_33@ Public Sub New() powerLevel = 0 name = "Super Bison" End Sub Public Sub New(ByVal powerLevel As Integer) Me.New("Super Bison") ' Call other constructor@H_301_33@ Me.powerLevel = powerLevel End Sub Public Sub New(ByVal name As String) MyBase.New(name) ' Call base classes' constructor@H_301_33@ Me.name = name End Sub Shared Sub New() ' Shared constructor invoked before 1st instance is created@H_301_33@ End Sub Protected Overrides Sub Finalize() ' Destructor to free unmanaged resources@H_301_33@ MyBase.Finalize() End Sub End Class |
class SuperHero : Person { |
||||||||||
|
|||||||||||
Dim hero As SuperHero = New SuperHero With hero hero.Defend("Laura Jones") Dim hero2 As SuperHero = hero ' Both reference the same object@H_301_33@ hero = Nothing ' Free the object@H_301_33@ If hero Is Nothing Then _ Dim obj As Object = New SuperHero ' Mark object for quick disposal@H_301_33@ |
SuperHero hero = new SuperHero(); // No "With" but can use object initializers@H_301_33@ hero.Defend("Laura Jones");
hero = null ; // Free the object@H_301_33@ if (hero == null) Object obj = new SuperHero(); using (StreamReader reader = File.OpenText("test.txt")) { string line; while ((line = reader.ReadLine()) != null) Console.WriteLine(line); } |
||||||||||
|
|||||||||||
Structure Student Dim stu As Student = New Student("Bob",3.5) |
struct Student { public string name; public float gpa; public Student(string name,float gpa) { this.name = name; this.gpa = gpa; } } Student stu = new Student("Bob",3.5f); |
||||||||||
|
|||||||||||
' Auto-implemented properties are new to VB10@H_301_33@ ' Traditional property implementation@H_301_33@ ' Read-only property@H_301_33@ ' Write-only property@H_301_33@ |
// Auto-implemented properties@H_301_33@ // Traditional property implementation@H_301_33@ // Read-only property@H_301_33@ // Write-only property@H_301_33@ |
||||||||||
|
|||||||||||
Delegate Sub MsgArrivedEventHandler(ByVal message As String) Event MsgArrivedEvent As MsgArrivedEventHandler ' or to define an event which declares a delegate implicitly@H_301_33@ AddHandler MsgArrivedEvent,AddressOf My_MsgArrivedCallback Imports System.Windows.Forms Dim WithEvents MyButton As Button ' WithEvents can't be used on local variable@H_301_33@ Private Sub MyButton_Click(ByVal sender As System.Object,_ |
delegate void MsgArrivedEventHandler(string message); event MsgArrivedEventHandler MsgArrivedEvent; // Delegates must be used with events in C#@H_301_33@
Button MyButton = new Button(); private void MyButton_Click(object sender,System.EventArgs e) { |
||||||||||
|
|||||||||||
' Enforce accepted data type at compile-time @H_301_33@ ' Subroutine can display any type of List @H_301_33@ ' Class works on any data type@H_301_33@ ' Limit T to only types that implement IComparable@H_301_33@ |
// Enforce accepted data type at compile-time @H_301_33@ // Function can display any type of List @H_301_33@ // Class works on any data type@H_301_33@ // Limit T to only types that implement IComparable@H_301_33@ |
||||||||||
|
|||||||||||
Dim nums() As Integer = {5,8,1,6} |
int[] nums = { 5,6 }; // Get all numbers in the array above 4@H_301_33@ var results = from value in nums where value > 4 select value; Console.WriteLine(results.Count()); // 3@H_301_33@ Console.WriteLine(results.First()); // 5@H_301_33@ Console.WriteLine(results.Last()); // 6@H_301_33@ Console.WriteLine(results.Average()); // 6.33333@H_301_33@ // Displays 5 8 6 @H_301_33@ foreach (int n in results) Console.Write(n + " "); results = results.Intersect(new[] {5,7}); // 5 6@H_301_33@ results = results.Concat(new[] {5,5}); // 5 6 5 1 5@H_301_33@ results = results.Distinct(); // 5 6 1@H_301_33@ Student[] Students = { new Student{ Name = "Bob",GPA = 3.5 }, new Student{ Name = "Sue",GPA = 4.0 }, new Student{ Name = "Joe",GPA = 1.9 } }; // Get an ordered list of all students by GPA with GPA >= 3.0@H_301_33@ var goodStudents = from s in Students where s.GPA >= 3.0 orderby s.GPA descending select s; Console.WriteLine(goodStudents.First().Name); // Sue@H_301_33@ |
||||||||||
|
|||||||||||
Console.Write("What's your name? ") |
Console.Write("What's your name? ");
|
||||||||||
|
|||||||||||
Imports System.IO ' Write out to text file@H_301_33@ ' Read all lines from text file@H_301_33@ ' Write out to binary file@H_301_33@ ' Read from binary file@H_301_33@ |
using System.IO; // Write out to text file@H_301_33@ // Read all lines from text file@H_301_33@ // Write out to binary file@H_301_33@ // Read from binary file@H_301_33@ |