This is a tad anal but does C# have a standard format when it comes to naming Variables, Classes and instances. for instance in Java I've always been "pushed" to name my classes starting with capital letters and any variable starts lowercase and is delimited by a capitol letter to signify it's a new word in the name e.g.,
in Java:
SuperAwesomeClass coolClass();
In C++:
Super_Awesome_Class cool_class();
basically is there a widely accepted format for C# I should use?
I find it helps me understand others code better if I attempt to keep the same formate as them
C# Workshop - Week 1 (Ch. 1 & 2) - Advanced
[edit: see above or below to the link for Microsoft's style/naming guide]
[Edited by - Telastyn on July 6, 2007 10:01:44 AM]
[Edited by - Telastyn on July 6, 2007 10:01:44 AM]
Quote: Original post by arkngelus
Sorry if my english is not good, is not my native language, by the way feel free to talk to me in spanish if you want. :P
I was reading Chapter 2 and the last part talks about pre-processor directives, why and when do I need to use them??
Welcome to the workshop, and your English is actually quiet good. It is a lot better then I would say about 80% of the people I have seen online and most of those are native English speakers.
The two most common reasons for pre-processor directives are; for debugging and for platform dependent compiles.
You can add # define Debug and then then use the #if Debug directives you can wrap statements that you only want when you are debugging. When you make the release build, these statement are not even part of the code.
For platform dependent compiles; say you are making an XNA game for Windows and the XBox 360. Would you want to have two completely different code bases or would it be easier to wrap the Windows or XBox 360 specific code with #if directives.
#Define XBox360
#If Windows
// Do Windows stuff...
#If XBox360
// Do XBox 360 stuff.
theTroll
The link Washu posted above (Design Guidelines for Class Library Developers) is what I use for style determination (the "Naming Guidelines" subsection in particular).
Anything not covered I make up.
Anything not covered I make up.
For those who don't want to search through the style guidelines, the basic premise is either Pascal case or Camel case.
Example of Pascal case: MyCoolClass
Example of Camel case: myCoolClass
For type names (such as classes and enums): Pascal case
For method names: Pascal case
For property names: Pascal case
For private variables: Camel case
For method arguments: Camel case
For local variables: Camel case
Everything else: Pascal case
I may have forgotten something, but I think that covers it all.
I think another good example for preprocessor directives is the #region / #endregion directives, which can help you organize your code. They don't actually get compiled (they are removed during compilation), but if you have a code editor that supports them (such as VS), you can expand/collapse the groups to help make your files more manageable.
Example of Pascal case: MyCoolClass
Example of Camel case: myCoolClass
For type names (such as classes and enums): Pascal case
For method names: Pascal case
For property names: Pascal case
For private variables: Camel case
For method arguments: Camel case
For local variables: Camel case
Everything else: Pascal case
I may have forgotten something, but I think that covers it all.
Quote:
Welcome to the workshop, and your English is actually quiet good. It is a lot better then I would say about 80% of the people I have seen online and most of those are native English speakers.
The two most common reasons for pre-processor directives are; for debugging and for platform dependent compiles.
You can add # define Debug and then then use the #if Debug directives you can wrap statements that you only want when you are debugging. When you make the release build, these statement are not even part of the code.
For platform dependent compiles; say you are making an XNA game for Windows and the XBox 360. Would you want to have two completely different code bases or would it be easier to wrap the Windows or XBox 360 specific code with #if directives.
#Define XBox360
#If Windows
// Do Windows stuff...
#If XBox360
// Do XBox 360 stuff.
theTroll
I think another good example for preprocessor directives is the #region / #endregion directives, which can help you organize your code. They don't actually get compiled (they are removed during compilation), but if you have a code editor that supports them (such as VS), you can expand/collapse the groups to help make your files more manageable.
Quote: Original post by TheTrollQuote: Original post by arkngelus
Sorry if my english is not good, is not my native language, by the way feel free to talk to me in spanish if you want. :P
I was reading Chapter 2 and the last part talks about pre-processor directives, why and when do I need to use them??
Welcome to the workshop, and your English is actually quiet good. It is a lot better then I would say about 80% of the people I have seen online and most of those are native English speakers.
The two most common reasons for pre-processor directives are; for debugging and for platform dependent compiles.
You can add # define Debug and then then use the #if Debug directives you can wrap statements that you only want when you are debugging. When you make the release build, these statement are not even part of the code.
For platform dependent compiles; say you are making an XNA game for Windows and the XBox 360. Would you want to have two completely different code bases or would it be easier to wrap the Windows or XBox 360 specific code with #if directives.
#Define XBox360
#If Windows
// Do Windows stuff...
#If XBox360
// Do XBox 360 stuff.
theTroll
Straight to the point thanks, by the way great workshop
Chapter 1 & 2 Review Questions
Greetings everyone. Since it's Friday I wanted to take a moment to provide to you the answers I had when writing the review questions for this week. Please look over the following answers and make sure it matches with yours. If it doesn't and you're confused about why not, please post your questions here or in in the beginner thread for Week 1.
As this week is largely an overview of the entire language and doesnt go into significant enough detail to warrant a large number of exercises, we'll keep it simple for this week. Next week, however...[wink]
1. Write a program which outputs your name to the console.
Greetings everyone. Since it's Friday I wanted to take a moment to provide to you the answers I had when writing the review questions for this week. Please look over the following answers and make sure it matches with yours. If it doesn't and you're confused about why not, please post your questions here or in in the beginner thread for Week 1.
- What is component-oriented programming?
- What are the keys to software components in C#?
- What is Garbage Collection?
- What is exception Handling?
- What benefits do type safety provide you in C#?
- What is a unified type system?
- What are the benefits of a unified type system?
- What's the purpose of namespaces?
- What directive allows unqualified use of the members of a namespace?
- What is the entry point of a C# Console application?
- What's the name of the C# Runtime libraries?
- What are the key organizational concepts in C#?
- What two things do C# assemblies contain and what form are they in?
- What happens to the IL instructions just before the code is executed?
- When are forward decelerations needed in C#?
- What are the two kinds of variable types in C# what's the difference?
- How are C#'s value types subdivided?
- How are C#'s reference types subdivided?
- What is the encoding of a C# character, and how much memory does a single character require?
- What are the sizes of each of the Simple Types, and their range of values?
- Which 5 categories of C# types are user-definable types?
- Give an overview of each of the 5 types.
- What is "boxing" and "unboxing"
- What are expressions?
- What is the relationship between operators and operands in an expression?
- What is an operator's precedence?
- What is operator overloading?
- What are the different kinds of C# statements, and what do they do??
- What is a class?
- Create an example class named Character. Dont worry about members, methods, or properties
- What operator is used to create a new instance of a class? What's the syntax to create a Character object?
- What are the different types of members of a class?
- What are the 5 types of Accessibility used on a class's members?
- What does inheritance mean?
- What is a field?
- What is the difference between a static and non-static field?
- What is a method of a class?
- What's the difference between static and non-static methods?
- What are parameters?
- What are return types?
- What does a signature of a method consist of?
- What are the 4 types of parameters and how are they used?
- What is a static method?
- What is an instance method?
- What's the difference between a virtual and non-virtual method?
- What does it mean to override a virtual method?
- What is an abstract method and when is it permitted?
- What is method overloading?
- What is the difference between an instance constructor and a static constructor?
- What is the primary difference between properties and fields?
- How do you make a property read-only?
- What is an indexer?
- What is an event, and what is true of it's type?
- What are event handlers?
- What is a class operator?
- What is a destructor?
- What's the main difference between classes and structs?
- What is an array?
- What is an array initializer?
- What is an interface, and what can it contain?
- What are enums?
- What is an enum's underlying type?
- What is a delegate?
- What is the purpose of attributes?
- What is a compilation unit in C#?
- What are the 3 steps in compiling a program?
- What encoding are C# files in? ie. ASCII?
- What is the purpose of lexical grammar?
- What is the purpose of syntactic grammar?
- What is contained within the first line of a grammar rule?
- When you see the suffix "opt" in a grammar rule, what does it mean?
- What are the five basic elements that make up the structure of a C# source file?
- Which of the 5 elements of a C# source file are relevant to the syntax of a C# file?
- What do line terminators do?
- What are the two forms of comments supported by C#, give examples.
- What are the 5 types of tokens?
- List ALL of the C# keywords
- What is a literal, and what are the 6 types of literals?
- Give an example of each type of literal.
- What is a verbatim literal string, and how is it created?
- What are the possible Pre-processing directive, what is their function, and what is their intended use?
Contemporary software design increasingly relies on software components in the form of self-contained and self-describing packages of functionality
Key to such components is that they present a programming model with properties, methods, and events; they have attributes that provide declarative information about the component; and they incorporate their own documentation
Garbage collection automatically reclaims memory occupied by unused objects;
exception handling provides a structured and extensible approach to error detection and recovery;
design of the language makes it impossible to have uninitialized variables, to index arrays beyond their bounds, or to perform unchecked type casts.
All C# types, including primitive types such as int and double, inherit from a single root object type.
all types share a set of common operations, and values of any type can be stored, transported, and operated upon in a consistent manner.
Namespaces provide a hierarchical means of organizing C# programs and libraries.
The 'using' directive.
A static method named "main."
The .NET Framework Library
Programs, namespaces, types, members, and assemblies.
Executable code in the form of Intermediate Language and symbolic information in the form of metadata
It is converted into processor-specific code by the JIT compiler of the .NET CLR.
Never.
Valye types and reference types. Value types store data directly, reference types store references to their data.
simple types, enum types, and struct types.
Class types, interface types, array types, and delegate types.
Unicode encoding. It requires 16-bits.
####
class, struct, interface, enum, and delegate.
A class type defines a data structure that contains data members (fields) and function members (methods, properties, and others). Class types support inheritance and polymorphism, mechanisms whereby derived classes can extend and specialize base classes.
A struct type is similar to a class type in that it represents a structure with data members and function members. However, unlike classes, structs are value types and do not require heap allocation. Struct types do not support user-specified inheritance, and all struct types implicitly inherit from type object.
An interface type defines a contract as a named set of function members. A class or struct that implements an interface must provide implementations of the interface’s function members. An interface may inherit from multiple base interfaces, and a class or struct may implement multiple interfaces.
An enum type is a distinct type with named constants. Every enum type has an underlying type, which must be one of the eight integral types. The set of values of an enum type is the same as the set of values of the underlying type.
A delegate type represents references to methods with a particular parameter list and return type. Delegates make it possible to treat methods as entities that can be assigned to variables and passed as parameters. Delegates are similar to the concept of function pointers found in some other languages, but unlike function pointers, delegates are object-oriented and type-safe.
The process of casting a C# value type to an object.
Expressions are constructed from operands and operators
Operators tell what operation to perform on the operands.
A rule which governs the order in which the operator is evaluated relative to other operators.
Operator overloading permits user-defined operator implementations to be specified for operations where one or both of the operands are of a user-defined class or struct type.
Blocks - Allows multiple other statements in place of a single statements
Decelerations - declare variables and constants
Expressions - Evaluate an expression.
Selection - Also called branching, changes the flow of execution
Iteration - Similar to branching, allows execution of the same code repeatedly with some variance
Jump - Similar to branching, changes the scope of execution
Try...catch - Used to catch exceptions
Checked/unchecked - Used for checking underflow and overflow in integral types
Lock - Use for mutually exclusive locks of data for thread safety
using - use to temporarily obtain resources and dispose of them.
A class is a data structure that combines state (fields) and actions (methods and other function members) in a single unit.
public class Character
{
}
The new operator
Character myCharacter = new Character();
Constants - The constant values associated with the class
Fields - The variables of the class
Methods - The computations and actions that can be performed by the class
Properties - The actions associated with reading and writing named properties of the class
Indexers - The actions associated with indexing instances of the class like an array
Events - The notifications that can be generated by the class
Operators - The conversions and expression operators supported by the class
Constructors - The actions required to initialize instances of the class or the class itself
Destructors - The actions to perform before instances of the class are permanently discarded
Types - The nested types declared by the class
public - Access not limited
protected - Access limited to this class and classes derived from this class
internal - Access limited to this program
protected internal -Access limited to this program and classes derived from this class
private Access limited to this class
Inheritance means that a class implicitly contains all members of its base class, except for the constructors of the base class
A field is a variable that is associated with a class or with an instance of a class.
A static field has exactly one memory storage location, which is shared by all instances of a class. non-static is a unique
memory address for each instance.
A method is a member that implements a computation or action that can be performed by an object or class.
Static methods are accessible from the class, while non-static are accessible from the instances of the class, or "objects."
list of arguments, which represent values or variable references passed to the method
specifies the type of the value computed and returned by the method
The signature of a method consists of the name of the method and the number, modifiers, and types of its parameters. The signature of a method does NOT include the return type.
Value, reference, output, and arrays. Value is for input parameters. Reference parameters are used for both input and output. Output parameters are used simply for outgoing parameter passing. Parameter arrays allow a variable number of arguments to be passed into the method.
A method declared with a static modifier. It does not operaote on a specific instance of a class, and can only access static members.
Any method created without the static modifier. It operates on specific instance of the class, and can access both static and non-static members.
When a virtual method is invoked, it's runtime type for the instance is invoked. For a non-virtual method, the compile-time type is invoked.
The method overrides an inherited virtual method with the same signature, thus providing a new implementation for that method.
A method with no implementation. It is only permitted in classes also declared abstract.
Method overloading permits multiple methods in the same class to have the same name, so long as they have unique signatures.
Instance constructors are called when an object is created, to initialize the object. Static constructors are called by the CLR when a class is loaded, and is used to initialize the class itself.
Properties do not denote storage locations, and instead provide accessors which specify the statements to be executed when their values are read or written to.
Dont provide a set method.
An indexer is a member that enables object to be indexed in the same way as an array. It is declared like a property, except the name is always "this" followed by a parameter list written between square brackets.
An event is a member that enables a class or object to provide notifications. The type must always be a delegate.
Event handlers are methods attached to an event using the += operator and removed with the -= operator.
Its a member that defines (re-defines) the meaning of applying a particular expression operator to instances of the class.
A member that implements the actions required to destruct an instance of a class - specifically non-garbage collected member data.
Structs are value types instead of the reference types, and can be created on the stack, rather than the heap. A struct directly stores the data of the struct, while a class stores a reference to an object. Also, structs do not support inheritance.
An array is a data structure that contains a number of variables of the same type that are accessed through computed indices.
A way of initializing the values within an array during construction of the array.
An interface defines a contract that can be implemented by classes and structs. An interface can contain methods, properties, events, and indexers.
enums are a value type which contain a set of named, symbolic constants.
All enums have an underlying integral type. Those that are not specified have default underlying type of 'int'.
A delegate represents references to methods with a particular parameter list and return type. Delegates make it possible to treat methods as variables which can be assigned and passed as parameters.
To allow user-defined types of declarative information that can be attached to program entities and retrieved at run-time.
It's a source file
1. Transformation
2. Lexical Analysis
3. Syntatic Analysis
Unicode
It defines how Unicode characters are combined to form line terminators, white space, comments, tokens, and pre-processing directives.
Defines how the tokens resulting from the lexical grammar are combined to form C# programs.
The non-terminal symbol being defined, followed by a colon.
It means the previous term is optional.
Line terminators, white space, comments, tokens, and pre-processing directives.
The tokens.
divide the characters of C# source files into lines.
Single line comments, which start with // and comment out everything else on the line...and...
Delimited comments, which are anything between /* and */, even spanning multiple lines.
Identifiers, Keywords, literals, operators, and punctuators.
abstract as base bool breakbyte case catch char checkedclass const continue decimal defaultdelegate do double else enumevent explicit extern false finallyfixed float for foreach gotoif implicit in int interfaceinternal is lock long namespacenew null object operator outoverride params private protected publicreadonly ref return sbyte sealedshort sizeof stackalloc static stringstruct switch this throw truetry typeof uint ulong uncheckedunsafe ushort using virtual voidvolatile while
A source code representation of an actual value.
boolean
integer
real
character
string
null
true
200
200.23
'c'
"Hello, World"
null
A verbatim literal is one with a @ at the beginning, and in a verbatim string, escape characters are not interpreted.
#define, #undef - defining and undefing symbols
#if, #elif, #else, #endif controling conditional compilation based on the existance of defined symbols
#line changing the line number and file name of source.
#error and #warning, used to generate compile-time warnings and errors
#region and #endregion used to group lines of source code for a programmer.
As this week is largely an overview of the entire language and doesnt go into significant enough detail to warrant a large number of exercises, we'll keep it simple for this week. Next week, however...[wink]
1. Write a program which outputs your name to the console.
using System;class Hello{ static void Main() { Console.WriteLine("My name is Jeromy Walsh"); }}
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Hello, I would like to know what are the diferences between Interface and abstract class.
I can create an Interface with methods signatures to inherit from a class, also I can create an abstract class with abstract methods and have it inherited the same way, how come?
thanks in advance!
I can create an Interface with methods signatures to inherit from a class, also I can create an abstract class with abstract methods and have it inherited the same way, how come?
thanks in advance!
blog: www.brasilokau.com/games/blog
In practise, the main difference is that a class can implent any number of interfaces, but only inherit from one class (abstract or not). An abstract class _can_ have method bodies, variables, etc, which interfaces cannot.
This has been brought up earlier in the thread, in much greater detail, few pages back.
This has been brought up earlier in the thread, in much greater detail, few pages back.
Quote: Original post by BrasiLokau
Hello, I would like to know what are the diferences between Interface and abstract class.
I can create an Interface with methods signatures to inherit from a class, also I can create an abstract class with abstract methods and have it inherited the same way, how come?
thanks in advance!
An interface is like a contractual obligation for classes to implement, and can never have any implementation detail in their definition. An exposed interface provides a method of interaction with a closed system without having to show any implementation detail to the outside world.
An abstract class is a partial implementation of something, where functionality that can be centralised is put. Sometimes the concept can be quite vague and abstract, hence the name.
Using a classic animal style metaphor...
IInteractWithAnimal interface might have methods "Follow", "Stop"
Mammal and Insect abstract classes that encapsulate common things to do with that class of animal.
Dog, Cat, Spider, Bee would extend the base classes above as appropriate to flesh out details that are specific to that type of animal.
Something could get an instance of an animal, and as long as that implements the IInteractWithAnimal interface know that no matter what it's base type or derived type is - that you can get the animal to follow you, or stop.
Now someone could argue that there might be an Animal base class that both Mammal and Insect inherit from too. Architectural decisions have pros and cons all of their own, and assignment of responsibility and deciding the level of abstraction in a class hierarchy are real art forms. If anyone wants to get a little taste of that level of thinking, look up some of the more simple "software design patterns" for ready-rolled and proven solutions that can be adapted to solve many real-life problems.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement