Adi's Blog

Jangan Salahkan Saya Jika Ilmu Anda Bertambah …

Perbandingan Source Code VB. Net dan C# .Net

Inilah mungkin sedikit perbandingan antara sintaks vb .net dengan c# .net. Menurut saya lebih keren sintaks c# .net dari pada vb .net, karena terlihat lebih “programmer”. Yah mungkin konversi dari vb .net ke c# .net jauh lebih susah, ketimbang c# .net ke vb .net, itu sih menurut saya, karena saya dulu programmer vb, tp saya sekarang lebih menyukai c# .net.

Program Structure

VB

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 passed from the command line
 If args.Length = 1 Then  name = args(0)

 Console.WriteLine("Hello, " & name & "!")
 End Sub
 End Class
 End Namespace

C#

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
 if (args.Length == 1)
 name = args[0];

 Console.WriteLine("Hello, " + name + "!");
 }
 }
 }

Comments

VB


‘ Single line only

REM Single line only

”’ <summary>XML comments</summary>

C#


// Single line

/* Multiple

line  */

/// <summary>XML comments on single line</summary>

/** <summary>XML comments on multiple lines</summary> */

Data Type

VB


Value Types

Boolean

Byte, SByte

Char

Short, UShort, Integer, UInteger, Long, ULong

Single, Double

Decimal

Date   (alias of System.DateTime)

Reference Types

Object

String

Initializing

Dim correct As Boolean = True

Dim b As Byte = &H2A   ‘hex or &O52 for octal

Dim person As Object = Nothing

Dim name As String = “Dwight”

Dim grade As Char = “B”c

Dim today As Date = #12/31/2010 12:15:00 PM#

Dim amount As Decimal = 35.99@

Dim gpa As Single = 2.9!

Dim pi As Double = 3.14159265

Dim lTotal As Long = 123456L

Dim sTotal As Short = 123S

Dim usTotal As UShort = 123US

Dim uiTotal As UInteger = 123UI

Dim ulTotal As ULong = 123UL

Implicitly Typed Local Variables

Dim s = “Hello!”

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

Dim hero = New SuperHero With {.Name = “Batman”}

Type Information

Dim x As Integer

Console.WriteLine(x.GetType())
 ‘ Prints System.Int32

Console.WriteLine(GetType(Integer))   ‘ Prints System.Int32

Console.WriteLine(TypeName(x))    ‘Prints Integer

Dim c as New Circle

If TypeOf c Is Shape Then _

Console.WriteLine(“c is a Shape”)

Type Conversion / Casting

Dim d As Single = 3.5

Dim i As Integer = CType(d, Integer)
 ‘ set to 4 (Banker’s rounding)

i = CInt(d)  ‘ same result as CType

i = Int(d)    ‘ set to 3 (Int
 function truncates the decimal)

Dim o As Object = 2

i = DirectCast(o, Integer)
 ‘ Throws InvalidCastException if type cast fails

Dim s As New Shape

Dim c As Circle = TryCast(s, Circle)
 ‘ Returns Nothing if type cast fails

C#


Value Types

bool

byte, sbyte

char

short, ushort, int, uint, long, ulong

float, double

decimal

DateTime   (not a built-in C# type)

Reference Types

object

string

Initializing

bool correct = true;

byte b = 0x2A;    // hex

object person = null;

string name = “Dwight”;

char grade = ‘B’;

DateTime today = DateTime.Parse(“12/31/2010 12:15:00 PM”);

decimal amount = 35.99m;

float gpa = 2.9f;

double pi = 3.14159265;

long lTotal = 123456L;

short sTotal = 123;

ushort usTotal = 123;

uint uiTotal = 123;

ulong ulTotal = 123;

Implicitly Typed Local Variables

var s = “Hello!”;

var nums = new int[] { 1, 2, 3 };

var hero = new SuperHero() { Name = “Batman” };

Type Information

int x;

Console.WriteLine(x.GetType());  //Prints System.Int32

Console.WriteLine(typeof(int));
// Prints System.Int32

Console.WriteLine(x.GetType().Name);   // prints Int32

Circle c = new Circle();

if (c is Shape)

Console.WriteLine(“c is a Shape”);

Type Conversion / Casting

float d = 3.5f;

i = Convert.ToInt32(d);
// Set to 4 (rounds)

int i = (int)d;     // setto 3 (truncates decimal)

object o = 2;

int i = (int)o;
// Throws InvalidCastException if type cast fails

Shape s = new Shape();

Circle c = s as Circle;
// Returns null if type cast fails

Biar lebih jelas silahkan lihat di sini

One response to “Perbandingan Source Code VB. Net dan C# .Net

  1. AlySangadji 30 Januari, 2011 pukul 5:39 am

    Mantabs gan…Nice infonya..🙂

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: