Page 3 of 3 FirstFirst 123
Results 21 to 28 of 28
  1. #21
    Join Date
    Oct 2006
    Posts
    1,548
    For example, in C#, EVERY object is passed by reference
    Thats not true
    Im fully aware that there are cases where the language will perform boxing, but there are also cases where it doesnt.
    [quote][\quote]

  2. #22
    Join Date
    Mar 2004
    Posts
    0
    Quote Originally Posted by wforl View Post
    Im fully aware that there are cases where the language will perform boxing, but there are also cases where it doesnt.
    Right, but your value types will be boxed automatically as soon as you try to do something un-value-type-like on it.

  3. #23
    Join Date
    Oct 2006
    Posts
    1,548
    You seem to be missing the point.
    [quote][\quote]

  4. #24
    Join Date
    Feb 2005
    Location
    Bellevue, WA
    Posts
    3,251
    Quote Originally Posted by wforl View Post
    So then what happens when you pass around items which are declared as a struct type or built int types like int, float, char etc without the ref keyword, which every c#book i read call that passing by value.
    Those are value types not reference types. Value types are not objects, they are values. Instances of reference types are objects.

    So every object is passed around by its reference. Every value is passed around by its value.

    You are correct in saying that pass-by-value is what is indeed happening because in the object case you are passing the pointer around which is a copy of a memory address (thus by-value), but the effect is passing by reference; that is, you can update the instance of the object in the function body. Similarly, you cannot update the value of a value type without using the 'ref' keyword to force the reference (i.e. pointer) of that type to be passed into the function.

    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." ~Rich Cook

  5. #25
    Join Date
    Oct 2006
    Posts
    1,548
    Quote Originally Posted by owensd
    Those are value types not reference types.
    Again, which further clarifies my point that not ALL objects in c# are references.

    Quote Originally Posted by owensd
    Value types are not objects, they are values. Instances of reference types are objects.
    Quote Originally Posted by wikipedia
    In the domain of object-oriented programming an object is usually taken to mean a compilation of attributes (object elements) and behaviors (methods or subroutines) encapsulating an entity.

    Wikipedia
    And yes I have taken note of the "Usually" in that sentence, but to me, a struct can be exactly that, and the C# documentation seems to agree.

    Quote Originally Posted by Microsoft c# guide
    Objects are programming constructs that have data, behavior, and identity. Object data is contained in the fields, properties, and events of the object, and object behaviors are defined by the methods and interfaces of the object.

    Objects have identity — two objects with the same set of data are not necessarily the same object.

    Objects in C# are defined through classes and structs — these form the single blueprint from which all objects of that type operate.

    Objects have the following properties:

    * Everything you use in C# is an object, including Windows Forms and controls.
    * Objects are instantiated; that is, they are created from templates defined by classes and structs.
    * Objects use Properties (C# Programming Guide) to obtain and change the information they contain.
    * Objects often have methods and events that allow them to perform actions.
    * All C# objects inherit from the Object.

    C# Programming Guide

    But anyway. You can argue definitions all day long. Im just going by what the C# documentatio says itself.
    Last edited by wforl; 12-08-2010 at 11:43 AM.
    [quote][\quote]

  6. #26
    Join Date
    Feb 2005
    Location
    Bellevue, WA
    Posts
    3,251
    Well unfortunately the MSDN docs are self contradictory. Whatever you want to call them, you get seemingly different behavior with value-types and reference-types.

    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." ~Rich Cook

  7. #27
    Join Date
    Jun 2005
    Location
    Phoenix, Arizona
    Posts
    2,076
    Quote Originally Posted by dodlegion View Post
    Im converting some C++ code to c# and I need two things. I need vector equvilant container and I need to know to 'pass by reference' since '&' dosent work. Also is there already 2D dynamic array available or is that something Ill have to write myself? Thanks.
    Seeing as nobody seemed to answer dodlegion's questions I'll throw some out there for him.

    1. A List<T> should suit your needs for a vector replacement in C#.
    2. Take a look at the out and ref keywords and that should help you with passing by references.
    3. Take a look at the System.Collections.Generic namespace and might find something to suit your needs for a 2D dynamic array.

  8. #28
    Join Date
    Mar 2004
    Posts
    0
    Quote Originally Posted by Grinch78 View Post
    1. A List<T> should suit your needs for a vector replacement in C#.
    It is important to know that the System.Collections.Generic.List<T> uses the same storage mechanism as an std::vector (dynamically resizing array) whereas a System.Collections.Generic.LinkedList<T> uses the same storage mechanism as an std::list (linked list). As a result, a List<T> incurs the same performance penalties when adding items in the cases in which it needs to allocate a new block of contiguous memory. On the flipside, you can access List<T>'s via their index

    example:
    Code:
    var list = new List<int>();
    list[0] = 1;
    Whereas you cannot do the same on a linked list

    example:
    Code:
    var linkedList = new LinkedList<int>();
    linkedList[0]; // compiler error
    That being said, the "foreach" loop relies on an iterator (System.Collections.Generic.IEnumerable<T>) and accessing a linked list and a list via a foreach block has the same performance.

    Because many of the operations that you need to perform on a collection (especially all of LINQ's methods) simply relies on an IEnumerable<T> anyway, I often use LinkedLists as my collection of choice, seeing as how the usage almost never changes between that and a List<T>.

    2. Take a look at the out and ref keywords and that should help you with passing by references.
    Using these keywords leaves a bad taste in my mouth. They are only needed when working with value types, such as structs or enumerations. However, they really do allow for some nasty things to happen and I would discourage their use. If you need to return a value type, use the return functionality of a method. If you need to return multiple value types, consider creating a value type that composites the necessary data - OR - use a System.Tuple<T1, T2, ...>.

    discouraged (by me, so take this with a grain of salt)
    Code:
    void MutateIntAndFloat(ref int one, ref float two) 
    { 
        one += 10; 
        two += 20; 
    }
    
    void Main()
    {
        var one = 5; 
        var two = 2.5f;
        MutateIntAndFloat(ref one, ref two);
    }
    encouraged (again, by me)
    Code:
    Tuple<int, float> ReturnIntAndFloat(int one, float two)
    {
        return Tuple.Create(one + 10, two + 20);
    
        // OR (more verbose)
        return new Tuple<int, float>(one + 10, two + 20);
    }
    
    void Main()
    {
        var retVal = ReturnIntAndFloat(10, 20);
    }
    Documentation: http://msdn.microsoft.com/en-us/libr...tem.tuple.aspx

    3. Take a look at the System.Collections.Generic namespace and might find something to suit your needs for a 2D dynamic array.
    Again, this is another area where there are often better solutions provided to you by C# and the .net framework then defaulting on what you may be accustom to using on other languages. Many times when you need a 2D array, you can either do:

    If you need an std::map
    Code:
    var dict = new Dictionary<int, string>();
    dict[1024] = "Hello";
    dict[4324] = "World";
    If you don't need an std::map
    Code:
    var items = new LinkedList<Tuple<int, string>>();
    items.AddLast(Tuple.Create(1024, "Hello"));
    items.AddLast(Tuple.Create(4324, "World"));
    Documentation: http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

    Anyhow, I hope this clears some stuff up. Remember to try to investigate how to solve problems in C# rather then trying to solve problems as you would in C++, and shoehorn C# into something that it isn't suited for

Page 3 of 3 FirstFirst 123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •