473,378 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

(byte)0 - What exactly does this syntax mean?

Hi. I'm a VB.NET guy trying my best to learn C#.NET. I have an existing C#
application I'm trying to understand, and there is one piece of syntax I
can't wrap my head around. It is:

(byte)0

What exactly does this mean in C#? I can't find a VB.NET equivelent.

If someone could help me out I'd appreciate it.

Thanks!
Jun 27 '08 #1
7 1902
On May 28, 4:53 pm, Mike <M...@discussions.microsoft.comwrote:
Hi. I'm a VB.NET guy trying my best to learn C#.NET. I have an existing C#
application I'm trying to understand, and there is one piece of syntax I
can't wrap my head around. It is:

(byte)0

What exactly does this mean in C#? I can't find a VB.NET equivelent.

If someone could help me out I'd appreciate it.

Thanks!
CType(0, Byte)
i think you would need that in vb.net only if have
Option Strict On
Jun 27 '08 #2
The line of code you provided casts type int to byte. Generally the way you
type cast in c# is:

DataTypeY variableY = (DataTypeY)variableX;

The VB equivelant would be:

Dim x as DataTypeX

y = CType(x, DataTypeY)

It's been a while since I did any VB.NET, so I could be off. Google VB.NET
CType function.


"Mike" wrote:
Hi. I'm a VB.NET guy trying my best to learn C#.NET. I have an existing C#
application I'm trying to understand, and there is one piece of syntax I
can't wrap my head around. It is:

(byte)0

What exactly does this mean in C#? I can't find a VB.NET equivelent.

If someone could help me out I'd appreciate it.

Thanks!
Jun 27 '08 #3
On Wed, 28 May 2008 13:53:01 -0700, Mike <Mi**@discussions.microsoft.com>
wrote:
Hi. I'm a VB.NET guy trying my best to learn C#.NET. I have an
existing C#
application I'm trying to understand, and there is one piece of syntax I
can't wrap my head around. It is:

(byte)0
Where do you see that?

The other replies are basically correct, but...0 (and any other int within
0 to 255) is implicitly convertable to byte. It shouldn't be necessary to
cast the literal 0 to byte.

Pete
Jun 27 '08 #4
That explains it - Thanks!!

"Lenn" wrote:
The line of code you provided casts type int to byte. Generally the way you
type cast in c# is:

DataTypeY variableY = (DataTypeY)variableX;

The VB equivelant would be:

Dim x as DataTypeX

y = CType(x, DataTypeY)

It's been a while since I did any VB.NET, so I could be off. Google VB.NET
CType function.


"Mike" wrote:
Hi. I'm a VB.NET guy trying my best to learn C#.NET. I have an existing C#
application I'm trying to understand, and there is one piece of syntax I
can't wrap my head around. It is:

(byte)0

What exactly does this mean in C#? I can't find a VB.NET equivelent.

If someone could help me out I'd appreciate it.

Thanks!
Jun 27 '08 #5
Peter Duniho <Np*********@nnowslpianmk.comwrote:
Hi. I'm a VB.NET guy trying my best to learn C#.NET. I have an
existing C#
application I'm trying to understand, and there is one piece of syntax I
can't wrap my head around. It is:

(byte)0

Where do you see that?

The other replies are basically correct, but...0 (and any other int within
0 to 255) is implicitly convertable to byte. It shouldn't be necessary to
cast the literal 0 to byte.
The exception being when you want to use a byte overload instead of an
int overload, of course:

using System;

public class Test
{
static void Main()
{
Foo(0);
Foo((byte)0);
}

static void Foo(int i)
{
Console.WriteLine("int");
}

static void Foo(byte i)
{
Console.WriteLine("byte");
}
}

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #6
Jon,

Good point. Here's an interesting one too (well, I found it interesting
although I'm sure it is documented somewhere):

using System;

public class Test
{
static void Main()
{
Foo(0);
}

static void Foo(float x)
{
Console.WriteLine("float");
}

static void Foo(double x)
{
Console.WriteLine("double");
}
}

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Peter Duniho <Np*********@nnowslpianmk.comwrote:
Hi. I'm a VB.NET guy trying my best to learn C#.NET. I have an
existing C#
application I'm trying to understand, and there is one piece of syntax
I
can't wrap my head around. It is:

(byte)0

Where do you see that?

The other replies are basically correct, but...0 (and any other int
within
0 to 255) is implicitly convertable to byte. It shouldn't be necessary
to
cast the literal 0 to byte.

The exception being when you want to use a byte overload instead of an
int overload, of course:

using System;

public class Test
{
static void Main()
{
Foo(0);
Foo((byte)0);
}

static void Foo(int i)
{
Console.WriteLine("int");
}

static void Foo(byte i)
{
Console.WriteLine("byte");
}
}

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

Jun 27 '08 #7
On May 29, 8:34 am, "Hilton" <nos...@nospam.comwrote:
Good point. Here's an interesting one too (well, I found it interesting
although I'm sure it is documented somewhere):
It's in the spec in section 7.4.3.4 (C# 3.0 spec). There's an implicit
conversion from int to float, int to double, and float to double.

The last of these means that the conversion from int to float is
"better than" the conversion from int to double.

Jon
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jusovsky | last post by:
Hi guys. I'm parsing an XML document with the Chilkat XML.net parser. I am trying to take the easy way out since I'd rather not have to write my own. The problem is that the Chilkat parser...
29
by: Ed Morton | last post by:
I have 2 counters - one is required to be a 2-byte variable while the other is required to be 3 bytes (not my choice, but I'm stuck with it!). I've declared them as: unsigned short small;...
289
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
11
by: QQ | last post by:
I know a char is 2 bytes, the conversion is like byte byte_array = new byte; //Allocate double mem as that of char then for each char do byte = (byte) char & 0xff byte = (byte)( char >> 8 &...
3
by: Smugsboy | last post by:
Hello, I would like to know the managed C++ syntax of the following method (defined in C#): void temp(out byte byteArr). I tried something like: void temp( byte byteArr __gc)
3
by: Seion | last post by:
Hi, Any body can teach me how to convert double bytes HEX to single byte HEX? I'm not too sure what does this mean. But, it is a requirement from a server software that only allow single byte...
25
by: Charles Law | last post by:
I thought this was going to be straight forward, given the wealth of conversion functions in .NET, but it is proving more convoluted than imagined. Given the following <code> Dim ba(1) As...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
12
by: Daniel | last post by:
How do I change a byte variable so that I can add it's character value to the end of a variable of type string, such that: String^ s = "1234"; byte b; b= 53; s = s + b; thus s = "12345".
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.