Ingeniero
VB.NET
'Single line only Rem Single line only
C#
// Single line /* Multiple line */ /// XML comments on single line /** XML comments on multiple lines */
Program Structure
VB.NET
Imports System Namespace MyNameSpace Class HelloWorld 'Entry point which delegates to C-style main Private Function Public Overloads Shared Sub Main() Main(System.Environment.GetCommandLineArgs() )End Sub Overloads Shared Sub Main(args() As String) System.Console.WriteLine("Hello World") End Sub 'Main End Class 'HelloWorld End Namespace 'MyNameSpace
C#
using System Namespace MyNameSpace { class HelloWorld { static void Main(string[] args) { System.Console.WriteLine("Hello World") } } }
Data Types
VB.NET
'Value Types Boolean Byte Char (example: "A") Short, Integer, Long Single, DoubleDecimal Date 'Reference Types Object String
C#
//Value Types bool byte, sbyte char (example: 'A') short, ushort, int, uint, long, ulong float, double decimal DateTime //Reference Types object string
Dim x As Integer System.Console.WriteLine(x.GetType()) System.Console.WriteLine(TypeName(x)) 'Type conversion Dim d As Single = 3.5 Dim i As Integer = CType (d, Integer) i = CInt (d) i = Int(d)int x; Console.WriteLine(x.GetType()) Console.WriteLine(typeof(int)) //Type conversion float d = 3.5; int i = (int) d
Constants
VB.NET
Const MAX_AUTHORS As Integer = 25 ReadOnly MIN_RANK As Single = 5.00
C#
const int MAX_AUTHORS = 25; readonly float MIN_RANKING = 5.00;
Enumerations
VB.NET
Enum Action Start 'Stop is a reserved word [Stop] Rewind Forward End Enum Enum Status Flunk= 50 Pass = 70 Excel = 90 End Enum Dim a As Action = Action.Stop If a Action.Start Then _ 'Prints "Stop is 1" System.Console.WriteLine(a.ToString & " is " & a) 'Prints 70 System.Console.WriteLine(Status.Pass) 'Prints Pass System.Console.WriteLine(Status.Pass.ToStrin g()) Enum Weekdays Saturday Sunday Monday Tuesday Wednesday Thursday Friday End Enum 'Weekdays Action a = Action.Stop; if (a !=Action.Start) //Prints "Stop is 1" System.Console.WriteLine(a + " is " + (int) a); // Prints 70 System.Console.WriteLine((int) Status.Pass); // Prints Pass System.Console.WriteLine(Status.Pass); enum Weekdays { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday }
C#
enum Action {Start, Stop, Rewind, Forward}; enum Status {Flunk = 50, Pass = 70, Excel = 90};
Operators
VB.NET'Comparison = < > = 'Arithmetic + - * / Mod \ (integer division) ^ (raise to a power)
C#
//Comparison == < > = != //Arithmetic + - * / % (mod) / (integer division if both operands are ints)
Math.Pow(x, y) 'Assignment = += -= *= /= \= ^= = &= 'Bitwise And AndAlso Or OrElse Not > 'Logical And AndAlso Or OrElse Not 'String Concatenation & //Assignment = += -= *= /= %= &= |= ^= = ++ -//Bitwise & |^ ~ > //Logical && || ! //String Concatenation +
Choices
VB.NET
greeting = IIf(age < 20, "What's up?", "Hello") 'One line doesn't require "End If", no "Else" If language = "VB.NET" Then langType = "verbose" 'Use: to put two commands on same line If x 100 And y < 5 Then x *= 5 : y *= 2 'Preferred If x 100 And y < 5 Then x *= 5 y *= 2 End If if (x != 100 && y < 5) { // Multiple statementsmust be enclosed in {} x *= 5; y *= 2; }
C#
greeting = age < 20 ? "What's up?" : "Hello";
'or to break up any long single command use _ If henYouHaveAReally < longLine And _ itNeedsToBeBrokenInto2 > Lines Then _ UseTheUnderscore(charToBreakItUp) If x > 5 Then x *= y ElseIf x = 5 Then x += y ElseIf x < 10 Then x -= y Else x /= y End If 'Must be a primitive data type Select Case color
if (x> 5) x *= y; else if (x == 5) x += y; else if (x < 10) x -= y; else x /= y;
//Must be integer or string
Case "black", "red" r += 1 Case "blue" b += 1 Case "green" g += 1 Case Else other += 1 End Select
switch (color) { case "black": case "red": r++; break; case "blue" break; case "green": g++; break; default: other++; break; }
Loops
VB.NET
'Pre-test Loops: While c < 10 c += 1 End...
Regístrate para leer el documento completo.