简体   繁体   中英

C# Program Structure

I am still learning C# and have not done large programs but only console apps. I am reading Pro C# 3.5 and I have the following questions in my mind.

  1. I see that C# has enums, how are enums created on a large programs? Are they created in one C# file? or are they spread on different .cs file?

  2. Also, when I use Intellisense in C# express, I see that there is datatype string and String(with large S), are they the same? I check the object browser and I think they point to the same class.

  3. This is a fairly trivial question. I had programmed in java before, they have primitive datatype that does not have any inheritance hierarchy. But as I continue my exploration of C#, I notice that ints,double,float in C# has some inheritance hierarchy as they inherit from System.ValueType. Does this mean that C# is more OO(Object Oriented) than java? I am not sure of this though, because from the book, System.ValueType is not a concrete class.

Sorry, If my question might be vague but I am just a beginner trying to learn. Hope you have patience on me.

Thanks also for this site for suggesting that book, I really like the way it teaches the topics.

1.) Enums can be created anywhere you want, but it is always a good rule of thumb to put them where they make sense logically within a namespace. I usually have a file in the root of a namespace folder called Constants.cs or something that houses enums for that namespace.

2.) string maps to System.String so they are equivalent.

3.) Everything in the CLR derives from System.Object . There are essentially two types of objects in the CLR, Reference Types, and Value Types. All the primitive types, and enums are value types, that is why they are derived from System.ValueType

  1. You can create/declare enums in any existing .cs file or in a .cs file by itself (although I do recommend a separate file). It's the same as creating/declaring classes, structs, etc.

  2. String and string are the same type. string is the C# intrinsic type that maps to a CLR String type.

  3. Not sure what you mean by 'more OO'. Value types still use the stack and not the heap though.

I can take a stab at answering your questions:

  1. I try to keep my enums close to the place they are needed. Which is to say, that they are either in the same file they will be used (if only used in one or two locations) or the same folder. (Localization of common code.) For example, I recently made a NavalBattle game, and had an enum of tile types (deep water, shallow water, land) as well as an enum of command types (attack, move, surrender.) I found it much cleaner to go TileType.Land than having a larger enum with everything.

  2. They are the same.

  3. You may want to read: http://msdn.microsoft.com/en-us/library/ms228360(VS.80).aspx

1) Enums can be done anywhere you feel like it. They can be in the same file, a different file but part of the same class, I think you may even be able to place them inside Objects, but don't quote me on that.

2) They are the same, and I believe this answer ties into 3.

3) The primitives in C# are actually objects. Does this make it more object oriented than Java? That depends on your definition of Object Oriented. If you feel that what data types a language brings with it determine how OO a language is, then yes, C# is more object oriented than Java. If you feel its tied to how you write the code and how Objects work, then its up for debate, but I think they are about the same in that respect.

  1. Deciding where to define enums depends on the coding style you're using I prefer to keep the definition close to where it's used for it's primary processing.
  2. String and string are the same thing in the same way that Int32 is int, UInt32 is uint, and object is Object
  3. Everything in C# derives from Object, value types derive from ValueType which derives from Object. Things that derive from ValueType are treated specially, in that they may be kept on the stack though there is no guarantee of that. For more information Eric Lippert had a good discussion about the stack in C# at http://blogs.msdn.com/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx
  1. Generally, it's up to you, but you should try and make the placement complimentary to the classes that use the enum(s). As such, it's common to place enums in the same namespace (which ideally would mean the same project directory) as classes that use it. Alternatively, putting all of your enums in something along the lines of "ProjectNamespace.Constants" is not uncommon either. Just imagine when you are using a class that takes an enum as a parameter. Finding it should take almost no time at all.

  2. They're the same. Also, note that other similar shortcuts like "int" map to fully qualified names like System.Int32. It's important to realize these as well.

  3. Everything derives from System.Object and from there branches off into value and reference types. To get a handle around this, play around with creating a struct for an understanding of value types and a class for reference types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM