This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In C#, why is String a reference type that behaves like a value type?
Why in C# string is a class/ref type , where as int/double are value/struct- any specific reason or it is by design
Integral types have the important property of being accessible as a whole by the processor in one go. It is not the case for a string which may be composed of thousands of bytes, so in all languages, strings have always been pointed to, because the computer cannot really do it any other way.
In an object language like C#, it is canonical to create a class to point to a memory location: that's actually what an object is about.
So yes, strings are classes, because they can't be integral types.
Related
This question already has answers here:
Create dynamic variable name
(5 answers)
Closed 4 years ago.
I want to change the variable name to the string I entered.
For example :
public int MyVariable;
public string Name="MyName";
I want Rename 'MyVariable' to 'MyName'.How ?
You can't because C# is a strongly-typed language.
See Types (C# Programming Guide).
C# is a strongly-typed language. Every variable and constant has a
type, as does every expression that evaluates to a value. Every method
signature specifies a type for each input parameter and for the return
value. The .NET class library defines a set of built-in numeric types
as well as more complex types that represent a wide variety of logical
constructs, such as the file system, network connections, collections
and arrays of objects, and dates. A typical C# program uses types from
the class library as well as user-defined types that model the
concepts that are specific to the program's problem domain.
This question already has an answer here:
Why do I need an Interface for Covariance (out Type)?
(1 answer)
Closed 8 years ago.
Why in .Net templating a generic class is an invariant operation towards generic arguments?
Interfaces and delegates are not, but classes are.
For instance, I would like to be able to assign object of type Expression<Func<string>> to Expression<Func<object>>. As T in Func<T> is "out" and Expression is immutable, it would be reasonable to assign it as I have showed, right?
Had classes allowed variant type parameters, you wouldn't be able to use them in any field, since fields are always (at least sometimes) writable and readable.
That would limit the utility enough to make it not worth it.
This question already has answers here:
What's the difference between the 'ref' and 'out' keywords?
(28 answers)
Why ref and out in C#?
(7 answers)
Closed 9 years ago.
As per this post, the reason there is a distinction between ref and out is because it is costly to copy the value of the variable when using ref.
Why is there a need to marshall in the first place? Doesn't C# just pass the pointer under the hood? In that case, there would be no need to copy values.
Because the semantics of the two are completely different.
An out parameter is used to indicate that it will be used to return (output) a value, nothing more.
A ref parameter on the other hand indicates that an existing object (variable) should be passed to the method by reference. In the context of C#, an object passed by reference (not to be confused by reference types) is often a hint that the method will (and should) modify that object. It shouldn't be used "just because." It is generally used only for value types since it is the only way to get reference semantics for them.
This question already has answers here:
Determine a string's encoding in C#
(10 answers)
Closed 9 years ago.
I have a string read as a UTF8 (not from a file, can't check BOM).
The problem is that sometimes the original text was formed with another encoding, but was converted to UTF8 - so the string is not readable, sort of gibberish.
is it possible to detect that this string is not actual UTF8?
Thanks!
No. They're just bytes. You could try to guess, if you wanted, by trying different conversions and seeing whether there are valid dictionary words, etc., but in a theoretical sense it's impossible without knowing something about the data itself, i.e. knowing that it never uses certain characters, or always uses certain characters, or that it contains mostly words found in a given dictionary, etc. It might look like gibberish to a person, but the computer has no way of quantifying "gibberish".
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why are the properties of anonymous types in C# read-only?
I wrote something like this,
var suspense = new { Name = "Android", Market = string.Empty };
suspense.Market = "Potential";
.NET throws error
Property or indexer 'AnonymousType#1.Market' cannot be assigned to --
it is read only
I know that AnonymousTypes in C# are immutable, but why? Is this due to some limitation with CLR?
The motivating factor for driving the immutable anonymous types was because the LINQ APIs used hash tables internally and returning projections of anonymous types that could be modified was a dangerous situation.
You can check :
Immutable types: understand their benefits and use them
Anonymous Types and Object Identities By Tim Ng on MSDN