|
i'm getting "name can be simplified" since switching to VS2015 Community. (change String to string.)
|
|
|
|
|
Now that you have made up your mind, let me generate some doubts again.
It's no secret that value types don't really exist. They are classes, reference types, that are made to behave like value types by the compiler. You can use tham as reference types if you wish by working with the classes. If you want the value type behavior, you use the alias and the compiler does the rest.
The question which one to use is less a question of style. It's more a question of what you want to accomplish and strictly choosing one or the other limits your options without any real need.
The language is JavaScript. that of Mordor, which I will not utter here
This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a f***ing golf cart.
"I don't know, extraterrestrial?"
"You mean like from space?"
"No, from Canada."
If software development were a circus, we would all be the clowns.
|
|
|
|
|
Are you sure you're not thinking of Java?
With .NET, there's no difference in behaviour between using the class name or the alias.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Don't think so, when was the last time you had to initialize an int with new() before using it or check for null? Now try that with Int32, which without question is the class that corresponds to int.
The language is JavaScript. that of Mordor, which I will not utter here
This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a f***ing golf cart.
"I don't know, extraterrestrial?"
"You mean like from space?"
"No, from Canada."
If software development were a circus, we would all be the clowns.
|
|
|
|
|
|
I must admit that i have never tried to do that. Classes are reference types, (many) aliases are treated like value types. Always worked for me. Long ago I read a book when taking a look at .Net 1.0 and never had a need to question this.
The language is JavaScript. that of Mordor, which I will not utter here
This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a f***ing golf cart.
"I don't know, extraterrestrial?"
"You mean like from space?"
"No, from Canada."
If software development were a circus, we would all be the clowns.
|
|
|
|
|
CDP1802 wrote: Classes are reference types, (many) aliases are treated like value types.
Nope. There is absolutely no difference between using the type name (Int32 ) and the alias (int ). They are exactly the same thing. They compile to the same IL.
int x = new int();
Int32 y = 42;
Console.WriteLine($"{x}, {y}");
Java differentiates between the primitive types and their reference type equivalents. In .NET, there is no difference.
It sounds like whoever wrote that book was confused, and managed to spread the confusion.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
We already got that sorted out, I think. Int32 is not a class, it's a struct. Now it makes sense and you are right. It makes no difference.
The language is JavaScript. that of Mordor, which I will not utter here
This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a f***ing golf cart.
"I don't know, extraterrestrial?"
"You mean like from space?"
"No, from Canada."
If software development were a circus, we would all be the clowns.
|
|
|
|
|
structs are also classes...
(PIEBALD exits quickly...)
|
|
|
|
|
I know, but they are not treated as reference types and ...
I have a better idea. here its past 11 PM now. Do you have something to drink?
The language is JavaScript. that of Mordor, which I will not utter here
This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a f***ing golf cart.
"I don't know, extraterrestrial?"
"You mean like from space?"
"No, from Canada."
If software development were a circus, we would all be the clowns.
|
|
|
|
|
Do you feel how wrong you are?
modified 19-Jan-21 21:04pm.
|
|
|
|
|
That's the point I was making. It was just a style decision really.
- I would love to change the world, but they won’t give me the source code.
|
|
|
|
|
Value types fundamentally act differently in MSIL as well so it's not just a cheat. int is a real value type, it's even (along with most built-in types) extra special in the sense that it's built into MSIL directly. Int32 is not a reference type either.
String is a reference type, but so is string, it's just not very noticeable since it's mostly immutable.
|
|
|
|
|
Now it gets confusing. The MSDN does not mention any of this at first glance for the String class. They use string in their samples when they want to use it as a value type and describe how initializing a string with a literal leads to the generation of code that uses one of String's constructors in MISL.
The language is JavaScript. that of Mordor, which I will not utter here
This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a f***ing golf cart.
"I don't know, extraterrestrial?"
"You mean like from space?"
"No, from Canada."
If software development were a circus, we would all be the clowns.
|
|
|
|
|
Well it's built-in, so it's always going to be a bit odd.
Still, you can tell it's not a value type by cheating, if it was actually a value type it should be impossible to change the original, but as this experiment shows it is only made "impossible" by the immutability.
|
|
|
|
|
I have just gotten the answer to this little riddle. Int32 is a struct, not a class, while String really is a class and string is just treated as a value type.
The language is JavaScript. that of Mordor, which I will not utter here
This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a f***ing golf cart.
"I don't know, extraterrestrial?"
"You mean like from space?"
"No, from Canada."
If software development were a circus, we would all be the clowns.
|
|
|
|
|
It's not even that it's being treated like a value type. The reason it can accept literals like that is because of an implicit conversion operator.
public class ImplicitString
{
public string Value { get; private set; }
public ImplicitString(string x)
{
Value = x;
}
public static implicit operator ImplicitString(string x)
{
return new ImplicitString(x);
}
}
ImplicitString s = "Hello.";
|
|
|
|
|
|
Forogar wrote: It all compiles to the same IL code anyway so it was just a matter of style really.
That's all that needs to be said, right?
Forogar wrote:
What do you think?
I think it's getting late on Friday afternoon and I'm already past the point where I can afford to waste the brain cells I'd need for something like this...
|
|
|
|
|
|
I believe the reason it suggests to simplify the name is because string , int , etc do not require the System namespace include while String , Int32 , etc still do. It can help clean up your namespace includes if that file doesn't use the System namespace for things other than simple types I'm sure there are other reasons to suggest simplification but that's the one that immediately comes to mind.
EDIT: Now that I think about it, in a way the simple names "decouple" the developer from the exact underlying type too. They could transparently change int to map to Int64 in the future, for example.
|
|
|
|
|
Jon McKee wrote: They could transparently change int to map to Int64 in the future, for example They could, but won't. There are way too many things that would go crash-bang-BOOM if they did.
Software Zen: delete this;
|
|
|
|
|
True. More of a thought on possibility rather than implementation
|
|
|
|
|
Well, if someone ports .net to an 8-bit OS...
|
|
|
|
|
I used to do the same, string variable and String.Format(...) and int variable and Int32.Parse(...) .
There was a good reason I did that though, Microsoft recommended doing it!
Until I switched to Visual Studio 2015 and all of a sudden it started giving me "tips" to simplify String to string and Int32 to int ...
Thanks Microsoft, for sticking to your own guidelines
For the same reason I stopped using this and base (unless necessary) and TheClassImIn.StaticMethod instead of simply StaticMethod .
And yes, I know I can turn off those rules, but I like sticking to defaults
|
|
|
|