What is dfferent between C#.NET & VB.NET
Basic differences
- C # will be marked in the case identifiers and method names and class names, VB does not distinguish.
- VB is switched to late binding and early binding (Option Strict On / Off).
- VB is whether to switch to force a variable’s type (Option Explicit On / Off).
Literals
Not easily accomplished
string s = "Welcome";
char ch = 'abc';
int i = 123;
long l = 123L;
double d = 123.0;
float f = 123.0F;
bool bl = true;
object o = null; |
Dim s As String = "Welcome"
Dim ch As Char = "abc" c
Dim i As Integer = 123
Dim l As Long = 123L
Dim d As Double = 123.0
Dim f As Single = 123.0 F
Dim bl As Boolean = True
Dim o As Object = Nothing |
Date Literals
Dim d1 = # 05 / 01 / 2008 #
Dim d2 = # 05 / 01 / 2008 12: 00: 00 AM # |
I have another spare.
Array
Creation and access of the array
int [] array = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
/ / Skip
int [] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
/ / Type inference
var array = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
Console.WriteLine (array [0]);
Console.WriteLine (array [1]);
Console.WriteLine (array [2]); |
Dim array () As Integer = new Integer () {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
'Omitted
Dim array () As Integer () = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
'Type inference
Dim array () = New Integer () {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
Console. WriteLine (array (0));
Console. WriteLine (array (1));
Console. WriteLine (array (2)); |
Operator
The difference is in this together as
Equal Sign:
C# : ==
VB.NET =
Inequality:
C# : !=
VB.NET: <>
Statement
Conditional or repetitive statement
int int_val = Get_Value ();
/ / Conditional branch
if (int_val == 0) {
/ / True
} Else {
/ / False
}
/ / For statement
for (int i = 0; i <10; i + +) {
/ / Processing
}
int [] int_Array = Get_Array ();
/ / Statement (Collection)
foreach (int i in int_Array) {
/ / Processing
} |
Dim int_val As Integer = Get_Value ()
'Branches
If int_val = 0 Then
'True
Else
'False
End If
'Statement
For i As Integer = 0 to 10
'Treatment
End For
Dim int_Array As Integer (,) = Get_Array ()
'Statement (Collection)
For Each i As Integer in int_Array
'Treatment
Next |
Namespace
Namespace
namespace TheCodersSpace {
/ / Define
}
/ / Import
using TheCodersSpace;
/ / Alias
using TC = TheCodersSpace; |
Namespace TheCodersSpace
'Definition
End Namespace
'Import
Imports TheCodersSpace
'Alias
Imports TC = TheCodersSpace |
Import XML namespace
Imports <xmlns:TheCoders="http://code.venuthomas.net"> |
Class
Class (VB, the class also included in the module)
class TheCoders {
}
/ / Static class
static class StaticTheCoders {
}
/ / Abstract base class
abstract class AbstractTheCoders {
}
/ / Inheritance
class ExtendTheCoders: TheCoders {
}
/ / No inheritance
sealed class SealedTheCoders {
} |
Class TheCoders
End Class
'Static class (Module)
Module StaticTheCoders
End Module
'Base class
MustInherit Class AbstractTheCoders
End Class
'Inherited
Class ExtendTheCoders
Inherits TheCoders
End Class
'Not Inherited
NotInheritable Class SealedTheCoders
End Class |
Interface
Interface
interface ITheCoders {
}
/ / Implementation
class TheCoders: ITheCoders {
} |
Interface ITheCoders
End Interface
'Implementation
Class TheCoders
Implements ITheCoders
End Class |
Structure
Structure
struct TheCoders {
public string Name;
} |
Structure TheCoders
Public Name As String
End Structure |
Methods
Method (professional function)
public void TheCodersDoSomething () {
}
/ / In argument
public void TheCodersPrintMessage (string message) {
}
/ / Two arguments, the overload
public void TheCodersPrintMessage (string message, int count) {
}
/ / Return value with
public string TheCodersGetMessage () {
}
/ / Overrides
public virtual void TheCodersOverridableMethod () {
}
/ / Overrides
public override void TheCodersOverridableMethod () {
}
/ / Redefinition
public new public void TheCodersReDeclareMethod () {
} |
Public Sub TheCodersDoSomething ()
End Sub
'In argument
Public Sub TheCodersPrintMessage (ByVal message As String)
End Sub
'Two Arguments, Overloading
Public Sub TheCodersPrintMessage (ByVal message As String, ByVal count As Integer)
End Sub
'In Returns
Public Function TheCodersGetMessage () As String
End Function
'Can override
Public Overridable Sub TheCodersOverridableMethod ()
End Sub
'Override
Public Overrides Sub TheCodersOverridableMethod ()
End Sub
'Redefining
Public Shadows Sub TheCodersReDeclareMethod () {
} |
Properties
Properties
private string name;
/ / Properties
public string Name {
get {return name;}
set {name = value;}
}
/ / Read-only property
public string TheCodersReadOnlyName {
get {return name;}
} |
Private _name As String
'Properties
Public Property Name () As String
Get
Return _name
End Get
Set (ByVal value As String)
_name = value
End Set
End Property
'Read-only property
Public ReadOnly Property TheCodersReadOnlyName () As String
Get
Return _name
End Get
End Property |
Generic
Generic
List<string> strList = new List<string>();
strList.Add ( "Venu");
strList.Add ( "Thomas");
/ / Class definition
class GenericTheCoders <t> {
public T heCodersGetValue () {
}
} |
Dim strList As List (Of String) = New List (Of String)
strList. Add ( "Venu")
strList. Add ( "Thomas")
/ / Class definition Class GenericTheCoders (Of T)
Public Function heCodersGetValue () As T
End Function
End Class |
Nullable Types
Nullable there.
Dim i As Integer = Nothing |
Type inference
Type inference, it is. NET 3.5 (C # 3.0, VB9) function from
var strVal = "Welcome";
var intVal = 1; |
Dim strVal = "Welcome"
Dim intVal = 1 |
Lambda expression
Lambda expression
Func <int, int, int> func = (a, b) => a * b;
func (2, 3); |
Dim func As Func (Of Integer, Integer, Integer) = Function (a, b) a * b
func (2, 3) |
Also write this
Dim func = Function (a As Integer, b As Integer) a * b |
VB.NET smarter than the type inference
Directives preprocessor
VB I was able to
# define TheCoders
# if TheCoders
/ / TheCoders
# else
/ / Not TheCoders
# endif |
# Const TheCoders = True
# If TheCoders
'TheCoders
# Else
'Not TheCoders
# End If |
Share on Facebook