Creating my on variable

G

Guest

Hi,

I'm Roby E Martins, VC, VB, C# NET , ASP NET and JAVA developer.

I'm trying to create my on variable in C# ( Roby.Integer ).
I create a struct with operator to receive and send the value.
But I have a doubt. Receiving a value in the struct will call the
implict operator INT to Roby.Integer and then I'm must create a new
Roby.Integer.
I don't want to create new instances every time I send a new value to
my variable ( for best performance).
So, How can I receive the values and just changing the value in the
memory, without sending my variable to a new instance all the time?

namespace Roby
{
public struct Integer
{
//Every thing will be stored here
private int Value;

public static implicit operator Roby.Integer( int Value )
{
//Receive a int variable
// THIS IS NOT GOOD, BUT IT LOOKS LIKE IS THE ONLY WAY
return new Roby.Integer( Value );
}

public static implicit operator int( Roby.Integer Value )
{
//Sending the value to a int variable
return Value.Value;
}
}
}
 
J

Jon Skeet [C# MVP]

Roby E Martins said:
I'm Roby E Martins, VC, VB, C# NET , ASP NET and JAVA developer.

I'm trying to create my on variable in C# ( Roby.Integer ).
I create a struct with operator to receive and send the value.
But I have a doubt. Receiving a value in the struct will call the
implict operator INT to Roby.Integer and then I'm must create a new
Roby.Integer.
I don't want to create new instances every time I send a new value to
my variable ( for best performance).
So, How can I receive the values and just changing the value in the
memory, without sending my variable to a new instance all the time?

As your type is a value type, it doesn't matter whether you create a
new value or not - it'll have the same memory characteristics.
 
G

Guest

Jon,

Ok, I tried a simple application to test your arguments.
I created a struct with a variable
And declared two structes as that struct.
The first with a value (=5) and second referent with the first one, so
everything that happends in the same memory space will be changed in the
second too.
Now I execute NEW in the first one.
Look what happend. The second still pointing to the same memory space
and have the last value that we had set (=5).
But the First one has 0 as value.
"NEW" always create a new instance in memory. To class and structs.
So, when I'm executing a new in the operator, I'm creating a new
instance from my variable(struct) and send the reference to the called
variable.
Calling ( a = 1) with execute ( (Operator ... ) return new
Roby.Integer(1) )

Sample:

namespace ConsoleApplication1
{
struct Roby
{
public int I;
}

class Program
{
static void Main( string[] args )
{
Roby Var;
Var.I = 5;
Roby Var2 = Var;

Var = new Roby();

System.Console.WriteLine( "VAR : {0}" , Var.I );
System.Console.WriteLine( "VAR2 : {0}" , Var2.I );
System.Console.Read();

}
}
}
 
G

Guest

Hi Jon,

One another thing.
I read your articles in your homepage about parameter variables.
I made all the microsoft courses about NET. and the teacher told us
that by value is faster then by reference. He told that NET using by value
simple copy the source value to a new instance in the method, but with by ref
NET create a variable copying the source and then sending it back. that's why
by value is default in NET.
Is that true??
 
R

Richard Blewett [DevelopMentor]

The

var = new Roby();

simply reinitializes the memory of var, it doesn't perform a new allocation.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.framework/<[email protected]>

Jon,

Ok, I tried a simple application to test your arguments.
I created a struct with a variable
And declared two structes as that struct.
The first with a value (=5) and second referent with the first one, so
everything that happends in the same memory space will be changed in the
second too.
Now I execute NEW in the first one.
Look what happend. The second still pointing to the same memory space
and have the last value that we had set (=5).
But the First one has 0 as value.
"NEW" always create a new instance in memory. To class and structs.
So, when I'm executing a new in the operator, I'm creating a new
instance from my variable(struct) and send the reference to the called
variable.
Calling ( a = 1) with execute ( (Operator ... ) return new
Roby.Integer(1) )

Sample:

namespace ConsoleApplication1
{
struct Roby
{
public int I;
}

class Program
{
static void Main( string[] args )
{
Roby Var;
Var.I = 5;
Roby Var2 = Var;

Var = new Roby();

System.Console.WriteLine( "VAR : {0}" , Var.I );
System.Console.WriteLine( "VAR2 : {0}" , Var2.I );
System.Console.Read();

}
}
}
 
J

Jon Skeet [C# MVP]

Roby E Martins said:
Ok, I tried a simple application to test your arguments.
I created a struct with a variable
And declared two structes as that struct.
The first with a value (=5) and second referent with the first one, so
everything that happends in the same memory space will be changed in the
second too.
Now I execute NEW in the first one.
Look what happend. The second still pointing to the same memory space
and have the last value that we had set (=5).
But the First one has 0 as value.
"NEW" always create a new instance in memory. To class and structs.

Yes - but so does just returning the value, for value types.

See http://www.pobox.com/~skeet/csharp/memory.html

Consider the "int" value type. Do you think there's a difference
between:

return i; // where i is a member variable
and
return 5*3; // creating a "new" int

?
 
J

Jon Skeet [C# MVP]

Roby E Martins said:
One another thing.
I read your articles in your homepage about parameter variables.
I made all the microsoft courses about NET. and the teacher told us
that by value is faster then by reference. He told that NET using by value
simple copy the source value to a new instance in the method, but with by ref
NET create a variable copying the source and then sending it back. that's why
by value is default in NET.
Is that true??

Well, for parameters passed by reference (in C# at least) there already
has to be a variable - no extra variable is created. Passing a
reference type by reference is slightly slower than passing it by value
due to the levels of indirection required. The choice should very
rarely be made on those grounds though - it should be in terms of the
semantics you want to use.

For value types, passing by reference can be faster if the value is
particularly large - it doesn't copy the source and then copy it back,
nor does it create a new instance (either for value types or reference
types).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top