I'm new to C# and wondering if 'types' are synonymous to 'objects' in Java. Am I understanding this correctly? From what I've learned so far, classes in C# define types, so by that definition they could be seen as objects.
3 Answers
A type in C# is a class
or a struct
.
In Java you only have classes or primitive types, so you almost always use the term class. But in C# you also have structs, which are value types, so we use the more generic term type to designate both.
Example:
var sb = new StringBuilder();
var id = Guid.NewGuid();
sb
is a variable holding a reference to an objectid
is a variable holding a valueStringBuilder
is a class, so it's also a typeGuid
is a struct, so it's also a type
There are other types as well: enums, delegates etc, bull all of those are just classes or structs in disguise: delegates become classes, and enums are structs.

- 50,214
- 11
- 107
- 158
-
@Ehsan Well... [nope](http://stackoverflow.com/a/5168165/3764814), at least not in the C# sense. – Lucas Trzesniewski Apr 10 '15 at 16:28
-
1
-
1
-
1Technically, I'd call `sb` and `id` variables, which can refer to an object or contain a value, respectively. A nitpick, but important to keep in mind when you talk about things like closures. – Dan Bryant Apr 10 '15 at 16:38
-
@Kjata30 right, but ultimately, a delegate is a class, and an enum is a struct. – Lucas Trzesniewski Apr 10 '15 at 16:40
-
1I would say there are two kind of types: value types and reference types. Structs and classes are particular kinds of those categories of types, respectively. Interface types are reference types, but not classes. Even this is oversimplification since it ignores pointer types, which are neither value nor reference types and can only occur in an unsafe context. Their inclusion might be confusing for someone asking a basic question like this. – Mike Zboray Apr 10 '15 at 18:08
-
@mikez yes, I think I simplified this a bit too much, I tried to answer in simple terms but I'll try to rewrite this te be more accurate - and indeed, I completely forgot pointer types in the answer, thanks for pointing that out. – Lucas Trzesniewski Apr 10 '15 at 19:02
If you declare a local variable string x
, then the variable x
can refer to an object instance of type string
. We call string
a reference type because variables contain references to an object instance.
Likewise, if you declare a local variable int y
, then the variable y
contains an instance of type int
. We call int
a value type because variables contain the actual value of an object instance.
One slight ambiguity comes in that object
is also the shorthand name in C# for the base type of all other types: System.Object
. So, in that sense, object
is used as the name of a type, but generally we use object to refer to the instances of a particular type.

- 27,329
- 4
- 56
- 102
"object" are often used to refer to the instances. object
is also the base class of any type in C#.
Types in C# are the type of object you are talking about. The difference becomes clear if you look at the following code snippet:
MyClass obj = new MyClass();
Type type = typeof(obj);
A type can be either a class (reference types; analogue to Java) or a struct (value types). Type is just the word that is used to refer to the complete set of them.

- 532
- 5
- 19