Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use #define?

Tags:

c#

I'm wondering about instances when it makes sent to use #define and #if statements. I've known about it for a while, but never incorporated it into my way of coding. How exactly does this affect the compilation?

Is #define the only thing that determines if the code is included when compiled? If I have #define DEBUGme as a custom symbol, the only way to exclude it from compile is to remove this #define statement?

like image 925
MaseBase Avatar asked Aug 19 '08 05:08

MaseBase


People also ask

How do you use a semicolon?

Use a semicolon to join two related independent clauses in place of a comma and a coordinating conjunction (and, but, or, nor, for, so, yet). Make sure when you use the semicolon that the connection between the two independent clauses is clear without the coordinating conjunction.

How do you use a colon and semicolon?

Colons (:) are used in sentences to show that something is following, like a quotation, example, or list. Semicolons (;) are used to join two independent clauses, or two complete thoughts that could stand alone as complete sentences.

Where do you use commas and semicolons?

When a comma separates two complete sentences joined by a conjunction (and, but, or, nor, for, so, or yet) the comma and the conjunction can be replaced with a semicolon. I ate dinner, and I went to the movies. = I ate dinner; I went to the movies. She finished top of her class, but she was struggling to find work.

How do you use dashes in a sentence?

Use dashes to mark the beginning and end of a series, which might otherwise get confused, with the rest of the sentence: Example: The three female characters—the wife, the nun, and the jockey—are the incarnation of excellence. Dashes are also used to mark the interruption of a sentence in dialogue: Example: “Help!


1 Answers

In C# #define macros, like some of Bernard's examples, are not allowed. The only common use of #define/#ifs in C# is for adding optional debug only code. For example:

        static void Main(string[] args)
        {
#if DEBUG
            //this only compiles if in DEBUG
            Console.WriteLine("DEBUG")
#endif 
#if !DEBUG
            //this only compiles if not in DEBUG
            Console.WriteLine("RELEASE")
#endif
            //This always compiles
            Console.ReadLine()
        }
like image 95
Eric Haskins Avatar answered Sep 21 '22 15:09

Eric Haskins