¡Bienvenid@ a La bloguera.net! Iniciar sesión | ÚNETE a la web | Ayuda





 
Migrar de VB.NET a C# o viceversa

  Por fin migramos en el curro a C# !!! (Lo siento Guille, se te van otros cuantos programadores a la competencia), ya podemos disfrutar del estandarte de .NET, con su estandar libre del ECMA .

   ¿El problema? todo lo que acarrea, sobre todo cambiar todo el codigo que normalmente usas, tus recetas, tus truquillos al nuevo lenguaje. Y en este busca que te busca me di de bruces con un articulo de el señor Frank McCown, el cual describe detalladamente las equivalencias de un leguaje a otro.

Comentarios

VB.NET

'Single line only

Rem Single line only

C#

// Single line

/* Multiple

line */

/// XML comments on single line

/** XML comments on multiple lines */

Esctructura del programa

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")

    }

  }

}

 

 

Tipos de datos

VB.NET

'Value Types

Boolean

Byte

Char (example: "A")

Short, Integer, Long

Single, Double

Decimal

Date

 

 

'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)

C#

//Value Types

bool

byte, sbyte

char (example: 'A')

short, ushort, int, uint, long, ulong

float, double

decimal

DateTime

 

 

//Reference Types

object

string

 

 

 

int x;

Console.WriteLine(x.GetType())

Console.WriteLine(typeof(int))

 

 

//Type conversion

float d = 3.5;

int i = (int) d

Constantes

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;

 

Enumeraciones

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.ToString())

 

 

Enum Weekdays

   Saturday

   Sunday

   Monday

   Tuesday

   Wednesday

   Thursday

   Friday

End Enum 'Weekdays

C#

enum Action {Start, Stop, Rewind, Forward};

enum Status {Flunk = 50, Pass = 70, Excel = 90};

 

 

 

 

 

 

 

 

 

 

 

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

}

 

 

Operadores

VB.NET

'Comparison

=  <  >  <=  >=  <>

 

 

'Arithmetic

+  -  *  /

Mod

\  (integer division)

^  (raise to a power)

 

 

'Assignment

=  +=  -=  *=  /=  \=  ^=  <<=  >>=  &=

 

 

'Bitwise

And  AndAlso  Or  OrElse  Not  <<  >>

 

 

'Logical

And  AndAlso  Or  OrElse  Not

 

 

'String Concatenation

&

C#

//Comparison

==  <  >  <=  >=  !=

 

 

//Arithmetic

+  -  *  /

%  (mod)

/  (integer division if both operands are ints)

Math.Pow(x, y)

 

 

//Assignment

=  +=  -=  *=  /=   %=  &=  |=  ^=  <<=  >>=  ++  --

 

 

//Bitwise

&  |  ^   ~  <<  >>

 

 

//Logical

&&  ||   !

 

 

//String Concatenation

+

 

Disyunciones

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

 

 

 

 

'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   

  Case "black", "red"

    r += 1

  Case "blue"

    b += 1

  Case "green"

    g += 1

  Case Else

    other += 1

End Select

C#

greeting = age < 20 ? "What's up?" : "Hello";

 

 

 

 

 

 

 

 

 

 

 

if (x != 100 && y < 5)

{

  // Multiple statements must be enclosed in {}

  x *= 5;

  y *= 2;

}

 

 

 

 

 

 

 

 

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

switch (color)

{

  case "black":

  case "red":    r++;

   break;

  case "blue"

   break;

  case "green": g++;  

   break;

  default:    other++;

   break;

}

 

 

Bucles

VB.NET

'Pre-test Loops:

While c < 10

  c += 1

End While Do Until c = 10

  c += 1

Loop

 

 

'Post-test Loop:

Do While c < 10

  c += 1

Loop

 

 

For c = 2 To 10 Step 2

  System.Console.WriteLine(c)

Next

 

 

 

'Array or collection looping

Dim names As String() = {"Steven", "SuOk", "Sarah"}

For Each s As String In names

  System.Console.WriteLine(s)

Next

C#

//Pre-test Loops: while (i < 10)

  i++;

for (i = 2; i < = 10; i += 2)

  System.Console.WriteLine(i);

 

 

 

 

 

 

//Post-test Loop:

do

  i++;

while (i < 10);

 

 

 

 

 

 

 

 

// Array or collection looping

string[] names = {"Steven", "SuOk", "Sarah"};

foreach (string s in names)

  System.Console.WriteLine(s);

 

 

Matrices

VB.NET

Dim nums() As Integer = {1, 2, 3}

For i As Integer = 0 To nums.Length -