A couple of semantic questions...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 29

Thread: A couple of semantic questions...

  1. #1
    Sean Woods Guest

    A couple of semantic questions...

    Okay... I have been working with C# for a while now and I am curious about a
    few things.

    First, when I code something like:
    public void TestFunction(ushort n)
    {
    for (ushort i = n - 1; i >= 10; i--)
    //do something here;
    }

    I get a compiler error that tells me that it can not implicityly convert
    type 'int' to 'ushort' on the i = n -1. What is up with that? Both i and n
    are ushorts... does C# automatically make make n-1 behave as if it were i =
    (int)(n-1)? Is there a nice way around it other than explictly casting n-1
    as a ushort as in i = (ushort)(n-1)?

    Second, Why is sizeof an unsafe operation? And further if sizeof is going
    to have to take a type instead of a variable then why can't I do something
    like sizeof(variable.GetType)? Is there some nice way to get the size of a
    variable.

    Finally, why can't a System.Array be used as the basis for an Indexer? I
    have a case where I don't want to declare the type on the Array until run
    time but I would like to use that Array as the basis for an Indexer. See
    the example below.

    public class SecBlock
    {
    private ushort size;
    private Array ptr;

    public SecBlock(ushort size, Type t)
    {
    this.size = size;
    ptr = Array.CreateInstance(t, size);
    }

    public object this[ushort column]
    {
    get
    {
    return ptr[column];
    }
    set
    {
    ptr[column] = value;
    }
    }
    }

    This give a compiler error stating that Array can't be used as a basis for
    an Indexer. Now, an ArrayList would be better because I need the array to
    be able to dynamically grow, but then the question becomes how do I assign
    the type to an ArrayList at runtime.

    Any help on any of these issues would be most appreciated.

    Thanks

    Sean Woods




  2. #2
    Jonathan Wood Guest

    Re: A couple of semantic questions...

    Sean,

    > I get a compiler error that tells me that it can not implicityly convert
    > type 'int' to 'ushort' on the i = n -1. What is up with that? Both i and

    n
    > are ushorts... does C# automatically make make n-1 behave as if it were i

    =
    > (int)(n-1)? Is there a nice way around it other than explictly casting

    n-1
    > as a ushort as in i = (ushort)(n-1)?


    That seems a bit lame don't it?

    Here's some info from the docs:

    Conversions
    There is a predefined implicit conversion from ushort to int, uint, long,
    ulong, float, double, or decimal.

    There is a predefined implicit conversion from byte or char to ushort.
    Otherwise a cast must be used to perform an explicit conversion. Consider
    for example, the following two ushort variables x and y:

    ushort x = 5, y = 12;
    The following assignment statement will produce a compilation error, because
    the arithmetic expression on the right-hand side of the assignment operator
    evaluates to int by default.

    ushort z = x + y; // Error: conversion from int to ushort
    To fix this problem, use a cast:

    ushort z = (ushort)(x + y); // OK: explicit conversion
    It is possible though to use the following statements, where the destination
    variable has the same storage size or a larger storage size:

    int m = x + y;
    long n = x + y;
    Notice also that there is no implicit conversion from floating-point types
    to ushort. For example, the following statement generates a compiler error
    unless an explicit cast is used:

    ushort x = 3.0; // Error: no implicit conversion from double
    ushort y = (ushort)3.0; // OK: explicit conversion
    For information on arithmetic expressions with mixed floating-point types
    and integral types, see float and double.

    For more information on implicit numeric conversion rules, see the Implicit
    Numeric Conversions Table.

    > Second, Why is sizeof an unsafe operation? And further if sizeof is going
    > to have to take a type instead of a variable then why can't I do something
    > like sizeof(variable.GetType)? Is there some nice way to get the size of

    a
    > variable.


    Don't know. Doesn't sizeof work with both types and variables?

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com



  3. #3
    Sean Woods Guest

    Re: A couple of semantic questions...

    Jonathon,

    > That seems a bit lame don't it?


    Yes... it does.

    > ushort x = 5, y = 12;
    > The following assignment statement will produce a compilation error,

    because
    > the arithmetic expression on the right-hand side of the assignment

    operator
    > evaluates to int by default.
    >
    > ushort z = x + y; // Error: conversion from int to ushort
    > To fix this problem, use a cast:
    >
    > ushort z = (ushort)(x + y); // OK: explicit conversion


    Yeah... that was exactly what I was doing, but it seems unneccessary to me.
    It seems that in the case above z = x + y should be fine and if x+y exceeds
    z's capacity then a runtime overflow error should occur, unless the explicit
    cast is done, in which case just shove what can fit of x-y into z.

    > > Second, Why is sizeof an unsafe operation? And further if sizeof is

    going
    > > to have to take a type instead of a variable then why can't I do

    something
    > > like sizeof(variable.GetType)? Is there some nice way to get the size

    of
    > a
    > > variable.

    >
    > Don't know. Doesn't sizeof work with both types and variables?


    Doesn't appear so. If you have:

    uint t = 5;
    Console.WriteLine(sizeof(t));

    You would expect to get back a 4 because a uint is 4 bytes but instead you
    get "(219): The type or namespace name 't' does not exist in the class or
    namespace..." I would have thought that this would be solved by doing
    something like:

    uint t = 5;
    Console.WriteLine(sizeof(t.GetType()));

    But I get the same error. If I do a sizeof(uint) though it gives me a 4.

    So how do I get the size of a variable? A .Size or .Length property would
    be nice.

    Sean



  4. #4
    Jonathan Wood Guest

    Re: A couple of semantic questions...

    Sean,

    > Yeah... that was exactly what I was doing, but it seems unneccessary to

    me.
    > It seems that in the case above z = x + y should be fine and if x+y

    exceeds
    > z's capacity then a runtime overflow error should occur, unless the

    explicit
    > cast is done, in which case just shove what can fit of x-y into z.


    Yeah, that's completely lame. I'm filling out a bug report on that. I
    recommend you do the same.

    > Console.WriteLine(sizeof(t));


    I don't know why they did it that way. It's almost as if you should avoid
    sizeof. The following seems to work just fine:

    Console.WriteLine(System.Runtime.InteropServices.Marshal.SizeOf(t));

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com



  5. #5
    Jonathan Wood Guest

    Re: A couple of semantic questions...

    Well, it returned the right value for a ushort variable. I guess sizeof does
    not.

    The error reported by the compiler says "sizeof can only be used in an
    unsafe context (consider using
    System.Runtime.InteropServices.Marshal.SizeOf)" so I figured pointing out
    that it seemed to work might be helpful.

    Is your position that there is no reliable way to get the size of a
    particular variable?

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com
    "Jeff Peil" <jpeil@bigfoot.com> wrote in message
    news:3a79be81@news.devx.com...
    >
    > "Jonathan Wood" <jwood@softcircuits.com> wrote in message
    > news:3a79b934@news.devx.com...
    > > > Console.WriteLine(sizeof(t));

    > >
    > > I don't know why they did it that way. It's almost as if you should

    avoid
    > > sizeof. The following seems to work just fine:
    > >
    > > Console.WriteLine(System.Runtime.InteropServices.Marshal.SizeOf(t));
    > >

    >
    > Well those *do* different things.
    >
    > The Marshal.SizeOf() call gives you the marshalled size
    > (post-transformations for a non-isomorphic type)
    >
    > The sizeof operator gives the internal size.
    >
    >




  6. #6
    Jeff Peil Guest

    Re: A couple of semantic questions...


    "Jonathan Wood" <jwood@softcircuits.com> wrote in message
    news:3a79b934@news.devx.com...
    > > Console.WriteLine(sizeof(t));

    >
    > I don't know why they did it that way. It's almost as if you should avoid
    > sizeof. The following seems to work just fine:
    >
    > Console.WriteLine(System.Runtime.InteropServices.Marshal.SizeOf(t));
    >


    Well those *do* different things.

    The Marshal.SizeOf() call gives you the marshalled size
    (post-transformations for a non-isomorphic type)

    The sizeof operator gives the internal size.



  7. #7
    Jeff Peil Guest

    Re: A couple of semantic questions...


    "Jonathan Wood" <jwood@softcircuits.com> wrote in message
    news:3a79bfc5@news.devx.com...
    > Well, it returned the right value for a ushort variable. I guess sizeof

    does
    > not.
    >
    > The error reported by the compiler says "sizeof can only be used in an
    > unsafe context (consider using
    > System.Runtime.InteropServices.Marshal.SizeOf)" so I figured pointing out
    > that it seemed to work might be helpful.
    >
    > Is your position that there is no reliable way to get the size of a
    > particular variable?


    No,

    It would be my position that you get what you ask for.

    The sizeof operator (which, as you noted, you can only use from an unsafe
    context) will give you the size of the type within the CLR

    The Marshal.SizeOf() method will give you the size of the type when
    marshalled. For an isomorphic type (like System.UInt16), these would be the
    same, but for a non-isomorphic type, they can differ.



  8. #8
    Eric Gunnerson Guest

    Re: A couple of semantic questions...

    Comments inline.
    "Sean Woods" <sdwood@ieee.org> wrote in message
    news:3a798edc@news.devx.com...
    > Okay... I have been working with C# for a while now and I am curious about

    a
    > few things.
    >
    > First, when I code something like:
    > public void TestFunction(ushort n)
    > {
    > for (ushort i = n - 1; i >= 10; i--)
    > //do something here;
    > }
    >
    > I get a compiler error that tells me that it can not implicityly convert
    > type 'int' to 'ushort' on the i = n -1. What is up with that? Both i and

    n
    > are ushorts... does C# automatically make make n-1 behave as if it were i

    =
    > (int)(n-1)? Is there a nice way around it other than explictly casting

    n-1
    > as a ushort as in i = (ushort)(n-1)?


    If you search through the language ref, you'll find that the operators +, -,
    *, and / are only defined for certain variable types. 7.7.5 says that the
    only versions of subtraction available require one of (int, int), (uint,
    uint), (long, long), or (ulong, ulong). Since 1 is an int, you'll get an int
    result, and you can't get from that back to i without an explicit cast.

    There's no nice way around it with different specifications; even if you
    specify 1 as an unsigned value '1u', you're still not going to be converting
    it implicitly back to a ushort. The fix is to use an int instead of a
    ushort; in C#, int is a much more natural type, and this is true in the
    frameworks as general. C# has unsigned types, but you would usually choose
    the signed version by default, and rarely go below an int in size.

    > Second, Why is sizeof an unsafe operation? And further if sizeof is going
    > to have to take a type instead of a variable then why can't I do something
    > like sizeof(variable.GetType)? Is there some nice way to get the size of

    a
    > variable.


    sizeof() is unsafe because the only times you need to know that information
    are in unsafe contexts.

    > Finally, why can't a System.Array be used as the basis for an Indexer? I
    > have a case where I don't want to declare the type on the Array until run
    > time but I would like to use that Array as the basis for an Indexer. See
    > the example below.
    >
    > public class SecBlock
    > {
    > private ushort size;
    > private Array ptr;
    >
    > public SecBlock(ushort size, Type t)
    > {
    > this.size = size;
    > ptr = Array.CreateInstance(t, size);
    > }
    >
    > public object this[ushort column]
    > {
    > get
    > {
    > return ptr[column];
    > }
    > set
    > {
    > ptr[column] = value;
    > }
    > }
    > }
    >
    > This give a compiler error stating that Array can't be used as a basis for
    > an Indexer. Now, an ArrayList would be better because I need the array to
    > be able to dynamically grow, but then the question becomes how do I assign
    > the type to an ArrayList at runtime.


    System.Array is the base type for all arrays, but is not an array type
    itself.

    You should just use an object[] instead of trying to be tricky. Since your
    indexer is of type object, you aren't gaining anything by trying to have
    another type of array internally.





  9. #9
    Jonathan Wood Guest

    Re: A couple of semantic questions...

    Eric,

    > If you search through the language ref, you'll find that the operators

    +, -,
    > *, and / are only defined for certain variable types. 7.7.5 says that the
    > only versions of subtraction available require one of (int, int), (uint,
    > uint), (long, long), or (ulong, ulong). Since 1 is an int, you'll get an

    int
    > result, and you can't get from that back to i without an explicit cast.


    I was seeing the same error with this:

    ushort i = 1;
    ushort j = 2;

    i = i + j;

    That shouldn't require a cast.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com



  10. #10
    Jonathan Wood Guest

    Re: A couple of semantic questions...

    Jeff,

    > > Is your position that there is no reliable way to get the size of a
    > > particular variable?

    >
    > No,
    >
    > It would be my position that you get what you ask for.
    >
    > The sizeof operator (which, as you noted, you can only use from an unsafe
    > context) will give you the size of the type within the CLR


    Right, but it was reportedly returning crazy numbers when used on a
    particular variable (rather than the type).

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com



  11. #11
    Gus Perez [MSFT] Guest

    Re: A couple of semantic questions...

    I'm not entirely sure, but I believe that beta 1 still might have allowed
    the usage of sizeof() on variables but in more current versions it only
    works on types. So I guess you can disregard the "crazy" numbers for now...

    "Jonathan Wood" <jwood@softcircuits.com> wrote in message
    news:3a79e124@news.devx.com...
    > Jeff,
    >
    > > > Is your position that there is no reliable way to get the size of a
    > > > particular variable?

    > >
    > > No,
    > >
    > > It would be my position that you get what you ask for.
    > >
    > > The sizeof operator (which, as you noted, you can only use from an

    unsafe
    > > context) will give you the size of the type within the CLR

    >
    > Right, but it was reportedly returning crazy numbers when used on a
    > particular variable (rather than the type).
    >
    > --
    > Jonathan Wood
    > SoftCircuits Programming
    > http://www.softcircuits.com
    >
    >




  12. #12
    Jonathan Allen Guest

    Re: A couple of semantic questions...

    FYI: Unsigned integers are not CLS compliant, so you shouldn't use them in
    public methods.

    --
    Jonathan Allen


    "Sean Woods" <sdwood@ieee.org> wrote in message
    news:3a798edc@news.devx.com...
    > Okay... I have been working with C# for a while now and I am curious about

    a
    > few things.
    >
    > First, when I code something like:
    > public void TestFunction(ushort n)
    > {
    > for (ushort i = n - 1; i >= 10; i--)
    > //do something here;
    > }
    >
    > I get a compiler error that tells me that it can not implicityly convert
    > type 'int' to 'ushort' on the i = n -1. What is up with that? Both i and

    n
    > are ushorts... does C# automatically make make n-1 behave as if it were i

    =
    > (int)(n-1)? Is there a nice way around it other than explictly casting

    n-1
    > as a ushort as in i = (ushort)(n-1)?
    >
    > Second, Why is sizeof an unsafe operation? And further if sizeof is going
    > to have to take a type instead of a variable then why can't I do something
    > like sizeof(variable.GetType)? Is there some nice way to get the size of

    a
    > variable.
    >
    > Finally, why can't a System.Array be used as the basis for an Indexer? I
    > have a case where I don't want to declare the type on the Array until run
    > time but I would like to use that Array as the basis for an Indexer. See
    > the example below.
    >
    > public class SecBlock
    > {
    > private ushort size;
    > private Array ptr;
    >
    > public SecBlock(ushort size, Type t)
    > {
    > this.size = size;
    > ptr = Array.CreateInstance(t, size);
    > }
    >
    > public object this[ushort column]
    > {
    > get
    > {
    > return ptr[column];
    > }
    > set
    > {
    > ptr[column] = value;
    > }
    > }
    > }
    >
    > This give a compiler error stating that Array can't be used as a basis for
    > an Indexer. Now, an ArrayList would be better because I need the array to
    > be able to dynamically grow, but then the question becomes how do I assign
    > the type to an ArrayList at runtime.
    >
    > Any help on any of these issues would be most appreciated.
    >
    > Thanks
    >
    > Sean Woods
    >
    >
    >




  13. #13
    Jonathan Wood Guest

    Re: A couple of semantic questions...

    Gus,

    > I'm not entirely sure, but I believe that beta 1 still might have allowed
    > the usage of sizeof() on variables but in more current versions it only
    > works on types. So I guess you can disregard the "crazy" numbers for

    now...

    Thanks. I guess we'll just have to revisit this once we've seen Beta 2.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com



  14. #14
    Jeff Peil Guest

    Re: A couple of semantic questions...


    "Jonathan Allen" <greywolfcs@bigfoot.com> wrote in message
    news:3a79ebe7@news.devx.com...
    > FYI: Unsigned integers are not CLS compliant, so you shouldn't use them in
    > public methods.


    Jonathan,

    Using them in a public method is fine. Exposing them in a public method's
    signature is what you want to avoid (and more specifically you can still do
    that, but if you do, and you want the class to be cls compliant that method
    needs to be specifically marked as not being cls compliant, and you should
    consider providing an alternative method that would be useful for languages
    that cannot use unsigned types.)



  15. #15
    Sean Woods Guest

    Re: A couple of semantic questions...

    "Eric Gunnerson" <ericgu@no.spam.microsoft.com> wrote in message
    news:3a79d4ac@news.devx.com...
    > There's no nice way around it with different specifications; even if you
    > specify 1 as an unsigned value '1u', you're still not going to be

    converting
    > it implicitly back to a ushort. The fix is to use an int instead of a
    > ushort; in C#, int is a much more natural type, and this is true in the
    > frameworks as general. C# has unsigned types, but you would usually choose
    > the signed version by default, and rarely go below an int in size.


    Unfortunately I am working with a situation (strong encryption) that
    requires the variables to be a certain size. I am not so much interested in
    a ushort as I am in something that holds 16 bits and treats them as unsigned
    in regard to shift operations. So using an int it out... I guess I will
    just be decorating my code with lots of explicit casts.

    > sizeof() is unsafe because the only times you need to know that

    information
    > are in unsafe contexts.


    I am not sure I agree. While sizeof may have uses in unsafe contexts it
    comes in handy when performing encryption. There are uses beyond working
    with pointers. But, I am not so much concerned with it being unsafe.

    I still would be curious as to how to determine the number of bytes a
    varible is occupying at runtime. It seems one of the following would be
    most logical, but they give compiler errors.

    Either
    ushort t = 5;
    sizeof(t.GetType());
    or even nicer would be
    object t = (ushort)5;
    sizeof(t.GetType());

    > > public class SecBlock
    > > {
    > > private ushort size;
    > > private Array ptr;
    > >
    > > public SecBlock(ushort size, Type t)
    > > {
    > > this.size = size;
    > > ptr = Array.CreateInstance(t, size);
    > > }
    > >
    > > public object this[ushort column]
    > > {
    > > get
    > > {
    > > return ptr[column];
    > > }
    > > set
    > > {
    > > ptr[column] = value;
    > > }
    > > }
    > > }

    >
    > You should just use an object[] instead of trying to be tricky. Since your
    > indexer is of type object, you aren't gaining anything by trying to have
    > another type of array internally.


    So what you are saying is in the above I should simply declare ptr like
    this:
    private object ptr;

    And then in the constructor for the class I can still do this:
    ptr = Array.CreateInstance(t, size);

    But trying that just leads to "(20): Cannot apply indexing with [] to an
    expression of type 'object'." Which is exactly the same thing I get when I
    use Array in the declaration. If I use object[] in the declaration then
    what I end up with is "(13): Cannot implicitly convert type 'System.Array'
    to 'object[]'."

    So how can I declare the array as an array of objects, let the user decide
    the type that the array will hold at run time and then ensure that type goes
    in there.

    In essence what I am looking for here is a way to set aside a block of
    memory that could either hold bytes, ushorts, uints, or ulongs. These
    blocks will be holding sensitive stuff. And as soon as the user knows that
    they are done with that block then a dispose method would come through and
    over write that block with 0s.

    Any more help would be greatly appreciated.

    Thanks in advance.
    Sean




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center