C
C#2w ago
Faker

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
Sehra
Sehra2w ago
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 Integerin java would be an int boxed as object
Faker
FakerOP2w ago
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?
Sehra
Sehra2w ago
if you look in the csproj, there is an ImplicitUsings that will pull in some common namespaces
Sehra
Sehra2w ago
in visual studio, you can see which namespaces are pulled in by clicking on the symbol in the top left corner
No description
sibber
sibber2w ago
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 boxed
Faker
FakerOP7d ago
alright, will have a look, thanks !

Did you find this page helpful?