The Basic Syntax - C# Programming Bootcamp: Learn the Basics of C# Programming in 2 Weeks (2016)

C# Programming Bootcamp: Learn the Basics of C# Programming in 2 Weeks (2016)

Chapter 3. The Basic Syntax

This chapter will discuss the syntax being used in C# programming. Study this material carefully since it can help you master this programming language within 2 weeks.

C# - An Object-Oriented Language

As discussed in the first chapter, the C# language is object-oriented. That means each program is composed of different objects that can communicate with each other. Objects may perform actions (known as“methods”).

To help you understand this concept, let’s analyze a simple object: a rectangle. The screenshot below shows the code for a Rectangle class. Let’s analyze this code while studying the basic syntax of C#:

After compiling and executing that code, you’ll get this result:

Now, let’s discuss the parts of that code one by one:

· using – All C# codes start with this line.“using” is a keyword that can link namespaces inside C# programs. Again, one program can contain several“using” lines. That means you can include multiple namespaces in each of your programs.

· class – You should use this keyword to declare classes.

· Comments– You should use this part to explain your code. In general, the C# compiler ignores this section. Make sure that your multiline comments begin with /* and end with */. Check the example below:

For single-line comments, on the other hand, use two slashes (i.e. //). Check the example below:

· member variables– These are the data members or attributes of the class. You should use variables to store data. For the current example, the class named Rectangle has 2 member variables: width and length.

· member functions– A function is a collection of commands that perform a certain job. In general, you should declare the functions of a class inside the class itself. In the program given above, Rectangle has 3 member functions: Display, GetArea, and AcceptDetails.


C# Identifiers

Identifiers are names used to determine classes, functions, variables, or any item defined by the programmer. Here are the rules you have to follow when naming classes in the C# language:

· Each name should start with a letter. You can continue the name using letters, numbers, and underscores. You can’t start an identifier’s name with a number.

· Your chosen name cannot contain spaces or special symbols (e.g. ?, +, %, and *). However, as mentioned in the previous rule, you can use underscores when naming your identifiers.

· You can’t use any C# keyword as an identifier’s name.

The Keywords of the C# Language

Basically, a keyword is a reserved word that has a predefined function in the C# language. You can’t use keywords in naming identifiers. If you really want to do that, however, you may add the“@” symbol at the beginning of the keyword.

In this programming language, some identifiers (e.g. set, get, etc.) have special meanings. Programmers refer to these identifiers as“contextual keywords.”

You will see two tables below. These tables will show you the contextual keywords and reserved keywords in the C# language.

The Contextual Keywords

set

get

let

add

group

alias

from

select

partial (method

global

partial (type)

dynamic

orderby

descending

ascending

into

remove

join

The Reserved Keywords

as

if

is

In

do

try

int

new

for

out

ref

goto

this

else

void

true

lock

char

byte

base

case

bool

long

null

uint

using

sbyte

catch

const

break

short

throw

ulong

while

event

class

float

false

fixed

decimal

abstract

checked

continue

delegate

explicit

internal

double

default

finally

in (as a generic modifier)

readonly

return

sealed

interface

implicit

static

virtual

switch

sizeof

namespace

params

foreach

protected

stackalloc

string

override

operator

unchecked

volatile

private

object

public

unsafe

extern

enum

typeof

ushort

out (as a generic modifier)

struct