Every class that I make requires the NEW keyword to instantiate it. I wonder then is Oledbconnection a class (because it requires a new keyword) ??? If it is then are int, bool, char also classes ??? I just need to clear my concepts.
-
Read [the manual](http://msdn.microsoft.com/en-us/library/vstudio/x9afc042.aspx). – Jon Oct 31 '12 at 13:02
-
Also - http://en.wikipedia.org/wiki/Common_Type_System – bryanmac Oct 31 '12 at 13:03
-
As well as [Types](http://msdn.microsoft.com/en-us/library/vstudio/ms173104.aspx) – chridam Oct 31 '12 at 13:05
-
Seriously guys, Can't I ask a question that was bugging me ??? I know I didn't structure it good enough but I wrote what I had a problem with ... Just because you guys know all that stuff doesn't mean you can mark a beginner's question as unclear. That's just a way to discourage my Love for programming .... And thanks everyone for answering this question no matter how stupid or easy it was for you guys. – Waqas Ali Oct 31 '12 at 13:22
-
The problem isn't that the idea of your question was bad (it seems reasonable enough to me), but that it's too vague. The way your question is worded doesn't make it immediately clear what you're asking, which is the main reason it was closed. – Tim Copenhaver Oct 31 '12 at 14:31
3 Answers
most types in .NET derive from object which is a class. So you could say that all those are classes but then some of those derive from ValueType which behave differently from reference types. And reference types are what most people refere to when talking about classes. In that respect Value type such as int, bool and any (other) struct are not classes.
an interfaces is neither a class nor a struct/ValueType it's an interface.
int x = 0; //the object represented by 0 is of type int which is a ValueType x.ToString(); //ToString is implemented by the base type Object which is a class (IComparable)x).CompareTo(1); //System.Int32 implements that interface
Boxing aside, the above illustrates that depending on how you see it an object might be classified as both ValueType, Class or interface
The real thing to worry about is not whether something is a class, an interface or something else. Because depending on how you see it most objects are in more than one set. A key thing to keep in mind is whether something has value semantics (structs/valuetypes) or reference semantics. When ever you assign something that has value semantics to something else a copy is made. If on the other hand you assign something that has reference semantics you are simply aliasing
public struct Foo{
public int x;
public Foo(int x){
this.x = x;
}
}
//value semantics
Foo x = new Foo(1)
Foo y = x;
//reference semantics
object xx = new object();
object yy = xx
Object.ReferenceEquals(x,y); // <-- returns false;
Object.ReferenceEquals(xx,yY); // <-- returns true;
If you are not careful then you can have some hard to find bugs when treating value types as if they had reference semantics or the other way around
public class Baz {
private Foo _myFoo = new Foo(1);
public Foo MyFoo {
get{
return _myFoo;
}
}
}
Foo bar = new Baz();
bar.MyFoo.x = 789; // <-- returning a copy of _myFoo not a reference to that object
if(barMyFoo.x == 1){
Console.WriteLine("This will be written to console because bar.x is 1!");
}

- 21,497
- 7
- 62
- 96
In OOP terms, those are all "classes". Whether or not they require the new keyword, value type mechanics, etc. are all implementation details of the C# language, not OOP concepts. At a conceptual level, yes, they are all classes in the OO sense.
In C#, the OOP term "class" is devided up into two types, normal classes and structs. Normal classes are treated as reference types, while structs are treated as value types (see here for a discussion of the difference).
The primitive types you mention (int, bool, char) are aliases to structs. That basically means behind-the-scenes, the compiler creates a new instance of the struct type for you. For example, int is an alias to System.Int32, so you can use the two interchangeably. This again is an implementation detail - it doesn't mean they're not classes, it just means the compiler is trying to simplify things and make life easier for you.

- 3,282
- 13
- 18
-
So you mean that after Objects there are two things ie. Reference types and value types . Classes are a subdivision of Reference types ??? and value types are a whole different thing ??? – Waqas Ali Oct 31 '12 at 13:10
-
1yes, basically. You have to be careful with the term "class". In OO terms, a class is just an entity with properties and behaviors that you create instances of. All of the things you listed fit that definition, so they're all classes in an OO sense. In C#, classes are divided into reference types and value types. To make things confusing, they call the reference types classes, even though both reference types and value types fit the OO definition of class. – Tim Copenhaver Oct 31 '12 at 13:15
-
-
OO does not have a concept of classes that in it self is a C# implementation detail (a detail it shares with other languages). it's object orientation not class orientation. JavaScript is object oriented (an functional) and there's no notion of `class` in JS. The same is true for SmallTalk – Rune FS Oct 31 '12 at 13:19
-
@RuneFS that's not true. OO principles specifically outline the theory of what a class is (see http://en.wikipedia.org/wiki/Object-oriented_programming or, more formally, http://dl.acm.org/citation.cfm?id=547964&dl=ACM&coll=portal). Also, JS is definitely not object oriented. – Tim Copenhaver Oct 31 '12 at 14:11
-
The first link specifically says "usually instance of class" so it follows not always class. Alan key who coined the term didn't talked about classes. Trygve Reenskauge the farther of MVC agrees that classes are not needed to do OO and there's an abundance of support for the statement that you can do OO in JavaScript https://www.google.dk/search?q=javascript+object+oriented&ie=UTF-8&oe=UTF-8&hl=da&client=safari. You can't do class oriented but then again I never claimed that – Rune FS Oct 31 '12 at 15:47
-
It says "usually" because no formal definition of OO has ever been agreed on, but there is no known definition of OO which does notuse classes. Perhaps you can provide an example of how inheritance, polymorphism, and encapsulation (the 3 commonly-agreed-upon keys of OO) without a concept of a class? On whether JS is OO, it fails based on the same 3 criteria (http://stackoverflow.com/questions/107464/is-javascript-object-oriented). You can trick Java into doing functional things if you try hard enough, but that doesn't make it a functional language. – Tim Copenhaver Oct 31 '12 at 16:03
-
Alan Kay had a very formal definition of OO where he equates any object to a representation of a small computer and any program as a network of interconnected systems. After all he's the guy who coined the term so he should know what he meant. The first several OO language such as Simula and SmallTalk did not have classes. Entire research is build on _object_ orieantation in contrast to class orientation an example is found here: http://www.artima.com/articles/dci_vision.html. – Rune FS Nov 01 '12 at 10:10
-
The accepted answer on JS being oo actually has this: "my vote still remains that Javascript is object oriented". He's counter example on inheritance/encapsulation is wrong the inheritance mechanism in JS is more flexible that that of class based but there's _nothing_ you can do with class based inheritance that you can't do with prototyped inheritance. For a reference ses Crockford: JavaScript the good parts – Rune FS Nov 01 '12 at 10:11
-
further the comment from Lie Ryan pretty much sums it all up. And is true to the definition to object orientation that Allan Kay gave it – Rune FS Nov 01 '12 at 10:17
-
Since there is no absolute agreement on what OO is, I suppose you are welcome to your opinions. You should know, though, that your definitions don't match what the vast majority of people mean when they discuss OO (as evidenced by that SO answer and a few minutes on Google). I believe we can leave it at that. – Tim Copenhaver Nov 01 '12 at 14:27
In C# we have "Value types" ie. int, double, decimal, enums, structs and "Reference types" - those are classes.

- 7,186
- 8
- 52
- 90
-
-
If interface is a data type then make an instance of an interface ;) – kubal5003 Oct 31 '12 at 14:44
-
Well int, double etc all implement interfaces so objects of those types can be see through interface types. Ie any int answers you challenges – Rune FS Oct 31 '12 at 14:49