Writing C Code


Writing C# Code

Before we start writing C# code, let's talk about some of the basic concepts of C# syntax.

To write C# code:

  • C# is a case-sensitive language. That means that if the help says a command is written AddTwoNumbers, you can't write addtwonumbers; it won't work. You have to make sure you match the case of each letter to the original command ( Figure 2.7 ).

    Figure 2.7 C# is case sensitive. That means the compiler won't understand a command unless you match the casing for each character to the original declaration. Normally commands follow the standard that each word is capitalized. The editor also helps you case things correctly as you enter code.
     void DoTask1() {    response.write("hello world"); //this is illegal  R  esponse.  W  rite("hello world"); //this is legal } 
  • Statements are terminated with a semicolon. In C# a single statement of code can be broken into many lines. The way the compiler knows you're done with a line is that you put a semicolon at the end. The semicolon isn't optional, it's mandatory ( Figure 2.8 ).

    Figure 2.8 Each statement must end with a semicolon. Other languages like VB don't use semicolons. However, VB suffers from the opposite problem; you have to put the entire statement in one line or use a special character if you want to break up lines.
     //these two statements are equivalent int x = 10 + 20 + c; int x     = 10     + 20     +     c; //these two statements have //errors, the are missing semicolons int y = 30 int z = 40 
  • Multiple statements are grouped within curly brackets. One example is functions, which are blocks of code that act as one unit. To define a block of code you enclose the statements that form the function inside curly brackets. Another example is if statements. In Chapter 3, "Conditionals and Loops," you will learn about if statements. An if statement can execute a single statement, or a block of statements, enclosed in curly brackets ( Figure 2.9 ).

    Figure 2.9 Curly brackets are used to create code blocks.
     //both "Response" statements are part of // the WriteLetter function void WriteLetter()  {  Response.Write("Dear Mr. William");    Response.Write("Thank you for your    recent inquiry.");  }  

graphics/tick.gif Tips

  • Many languages are case sensitive. The best-known exception is Visual Basic, which has always been case insensitive. In case-insensitive languages you don't have to be careful to match the casing of your code to the actual command. It may seem easier to be case insensitive, but Visual Basic has its own share of problems with this scenario. Sometimes the language gets confused when one developer cases a statement one way and another developer cases it a different way. In these situations, the language may not know that both developers meant to type the same statement.

  • Most statements of code can be split into multiple lines. The biggest exceptions are statements that contain strings ( Figure 2.10 ).

    Figure 2.10 You can't break up a string into multiple lines. The exception to that are special strings called literal strings. You will learn about these in Chapter 4, "Strings."
     //illegal to say string name = "Old Altered               Karate Frogs"; //must all be in one line when strings //are involved string name = "Old Altered Karate Frogs"; 
  • You don't have to use a semicolon when you use code blocks, only when you use single statements ( Figure 2.11 ).

    Figure 2.11 You don't have to put a semicolon at the end of a code block, only at the end of single statements.
     if (Age > 18)    Adult = true;  //semicolon used  if (Name == "James") {    Author = true;    Age = 32; }  // no semicolon at the end  



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net