Data types in C#
Hi guys, I'm currently learning C# with microsoft learn. I read elsewhere that C# contains 2 types of data types, primitive and referenced. I have a small background in Java. In java, we use the capitalized the first letter of any referenced data type because they are classes, like String. So, I was wondering if this is different in C#... I noticed that we declare a variable of type string as: string varName where the s is in lowercase.
6 Replies
c# splits them into value types (
struct
) and reference types (class
). string
is a type alias for System.String
, while int
is an alias for System.Int32
string is a reference type, int (and Int32) is a value type
closest to Integer
in java would be an int
boxed as object
yep I see, I will just stick with string instead of the System.String, by the way, behind the scenes I guess, System.String is being used, why "System" is implicitly used?
if you look in the csproj, there is an
ImplicitUsings
that will pull in some common namespacesin visual studio, you can see which namespaces are pulled in by clicking on the symbol in the top left corner
in c#
int
is just an alias, its the exact same thing as Int32
. if you want to box you explicitly cast to object
but since c# has real typed generics and not type erasure, generic types can be value types they dont need to be boxedalright, will have a look, thanks !