Let us have a Short look on syntax of the C# programming language.
Identifier
Identifier
An identifier is the name of an element in
the code.
An identifier can:
- start with an underscore: _
- contain an underscore: _
- contain a numeral: 0123456789
- contain both upper case and lower case Unicode letters. Case is sensitive (BOX is different from box).
An identifier cannot:
- start with a numeral
- start with a symbol, unless it is a keyword (check Keywords)
- have more than 511 chars
- contain @ sign in between or at the end
Keywords
Keywords are predefined reserved words with special syntactic
meaning.
|
Variables
Variables are identifiers associated with values. They are
declared by writing the variable's type and name, and are optionally
initialized in the same statement.
Declare
int myInt;
// Declaring an uninitialized variable called 'myInt', of type 'int'
Assigning
int myInt;
// Declaring an uninitialized variable
myInt = 35;
// Assigning the variable a value
Initialize
int myInt = 35;
// Declaring and initializing the variable
Constants
Constants are immutable values. and there
value does not change .They are initialized when declared.
const double PI = 3.14;
This shows all the uses of the keyword.
class xyz
{
const double X
= 3;
xyz()
{
const int
Y = 2;
}
}
readonly
The readonly keyword does a similar thing to
fields. Like fields marked as const they cannot change once initialized. The
difference is that you can choose to initialize them in a constructor. This
only works on fields. Read-only fields can either be members of an instance or
static class members.
Code blocks
The operators { ... } are used to signify a
code block and a new scope. Class members and the body of a method are examples
of what can live inside these braces in various contexts.
Inside of method bodies you can use the
braces to create new scopes like so:
void doSomething()
{
int a;
{
int b;
a = 1;
}
a = 2;
b = 3; // Will
fail because the variable is declared in an inner scope.
}
Program structure
A C# application consists of classes and
their members. Classes and other types exist in namespaces but can also be
nested inside other classes.
Main method
The
entry point of the C# application is the Main method. There can only be one,
and it is a static method in a class. The method usually returns void and is
passed command-line arguments as an array of strings.
static void Main(string[] args)
{
}
// OR Main method can be defined without parameters.
static void Main()
{
}
A Main method is also allowed to return an
integer value if specified.
static int Main(string[] args)
{
return 0;
}
Namespaces
Namespaces are a part of a type name and
they are used to group and/or distinguish named entities from other ones.
System.IO.DirectoryInfo // DirectoryInfo is in the
System.IO-namespace
A namespace is defined like this:
namespace AAANamespace
{
// Members
}
Some Commonly used Namespace
Using System; is a
statement to indicate that you are using a namespace called System. Basically
this namespace contains a class (Console) with methods needed to print the
values on the screen and read the values from the keyboard.
Using System.Collections.Generic is a statement to use Generic namespace that contains
interfaces and classes to allow the user to create strong types collections.
Using System.Linq; is a statement to use Linq namespace that contains
interfaces and classes that support query.
Using System.Text; is a statement to use Text namespace that contains
classes that represent ASCII, Unicode, UTF-7, and UTF-8 character encodings,
convert bytes to characters and vice versa, and manipulate and format string
objects.
For now the last three statements are not
important, but they will be used in the next sections of this tutorial.
For the console application, the program
will start from Main method of class Program that is contained in a namespace
(e.g. Ctest).
Console.WriteLine(“Hello World!”); is used to print the text “Hello World” on the screen.
Console.ReadLine(); is used to read a value from the keyboard.
Operators
Operator category
|
Operators
|
Arithmetic
|
+, -, *, /, %
|
Logical (boolean and bitwise)
|
&, |, ^, !, ~, &&, ||, true, false
|
String concatenation
|
+
|
Increment, decrement
|
++, --
|
Shift
|
<<, >>
|
Relational (conditional)
|
==, !=, <, >, <=, >=
|
Assignment
|
=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
|
Member access
|
.
|
Indexing
|
[]
|
Cast
|
()
|
Conditional
|
?:
|
Delegate concatenation and removal
|
+, -
|
Object creation
|
new
|
Type information
|
as is sizeof typeof
|
Overflow exception control
|
checked unchecked
|
Indirection and Address
|
*, ->, [], &
|
Coalesce
|
??
|
Lambda expression
|
=>
|
Comments
Post a Comment