0% found this document useful (0 votes)
29 views15 pages

L1 Csharp

The document discusses basic C# concepts like classes, methods, variables, data types, and identifiers. It defines what they are, how to declare them, and gives examples of their syntax and usage.

Uploaded by

Mielene Magura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views15 pages

L1 Csharp

The document discusses basic C# concepts like classes, methods, variables, data types, and identifiers. It defines what they are, how to declare them, and gives examples of their syntax and usage.

Uploaded by

Mielene Magura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

C#

 Class is a segment of code that you give a


name to

 (you can call them anything you like, as


long as C# hasn’t taken the word for
itself)
Method (Main)
This piece of code is something called a Method. The
name of the Method above is Main. When you run your
programme, C# looks for a Method called Main. It uses
the Main Method as the starting point for your
programmes. It then executes any code between those
two curly brackets. The blue words above are all
special words – keywords. You’ll learn more about
them in later chapters.
 Console.WriteLine("Hello C Sharp!");  Print “Hello C Sharp!
Declaration: A variable is declared by specifying its
type followed by its name. For example:

int number = 10;


string name = "Alice";
Constants: If you have a variable whose value will not change,
you can declare it as a constant using the “const” keyword. For
example:

 const double Pi = 3.14159;


Data Types

 Character
 String
 Boolean
 Integers
 Floating point
Variables
 Variables are use to store up temporary data to be
use in our in our runtime
Identifiers
 An identifier is a name given to a programming element, such as
a variable, function, class, module, or any other user-defined
item. Identifiers are used for identification purposes, making it
possible for programmers to refer to these elements in their code.

 Naming:

Identifiers must start with a letter (A-Z or a-z), an underscore (_),


or, in some languages, a non-ASCII character. After the first
character, identifiers can also include digits (0-9) and underscores.
Case Sensitivity:
In many programming languages, including C#, identifiers are case-
sensitive. This means that myVariable, MyVariable, and
MYVARIABLE would be considered different identifiers.

Purpose:
The primary purpose of identifiers is to provide a human-readable way
to reference the various elements of a program. This makes the code
more understandable and maintainable
Rules of Identifiers

int age = 25;


The rules for naming identifiers in programming languages are
essential for ensuring that code is syntactically correct and adheres to the
conventions of the language. While specific rules can vary between
programming languages, there are general guidelines that many languages
follow. Here are some common rules for identifiers:
Rules of Identifiers
 Starting Character:
Identifiers usually must begin with a letter (either uppercase or lowercase) or an underscore (_). Some
languages also allow identifiers to start with non-ASCII characters.

 Subsequent Characters:
After the first character, identifiers can consist of letters, digits (0-9), and underscores. The use of other
characters, like hyphens or spaces, is typically not allowed.

 Case Sensitivity:
Many programming languages, including C, C++, Java, and C#, treat identifiers as case-sensitive. This
means that Variable, variable, and VARIABLE would be considered distinct identifiers.

 No Reserved Words:
Identifiers cannot be the same as reserved words or keywords of the programming language. Reserved
words have special meaning in the language syntax, such as if, else, while, class, return, etc .
 using System; Syntax Example:
In the provided code, double salary = 80000.50; declares a
variable named salary of type double and initializes it with the
 class Program value 80000.50. This means that salary can store decimal
 { numbers, and in this case, it's being used to represent a
monetary value.
 static void Main()
 {
Syntax Example:
 int age = 30; In the provided code, bool isEmployed = true; declares a
 string name = "John Doe"; variable named isEmployed of type bool and initializes it with
 bool isEmployed = true; the value true. This implies that the variable isEmployed is

being used to represent the employment status of an individual,
double salary = 80000.50;
with true indicating that the individual is employed.

 Console.WriteLine("Name: " + name);


 Console.WriteLine("Age: " + age);
 Console.WriteLine("Employed: " + isEmployed);
 Console.WriteLine("Salary: $" + salary);
 }
 }
 using System;

 class Program The + operator inside the Console.WriteLine statement in C# is used for
string concatenation. String concatenation is the process of combining (or
 { appending) one string to the end of another string.
 static void Main()
 {
 int number1 = 5; // First number to add
 int number2 = 3; // Second number to add

 int sum = number1 + number2; // Adding two numbers

 Console.WriteLine("The sum of " + number1 + " and " + number2 + " is: " + sum);
 }
 }

You might also like