2.Basic Programming Conceptssss
2.Basic Programming Conceptssss
Member Functions
Functions are set of statements that perform a specific task.
Instantiating a Class
In the preceding program, the class ExecuteRectangle is used as a
class, which contains the Main() method and instantiates the Rectangle
class.
Basic Syntax
Identifiers
An identifier is a name used to identify a class, variable,
function, or any other user defined item. The basic rules for
naming classes in C# are as follows:
A name must begin with a letter that could be followed by a
sequence of letters, digits (0 - 9), or underscore. The first
character in an identifier cannot be a digit.
a) Local Variables
void ExampleMethod()
{
int num = 10; // Local variable
Console.WriteLine(num);
}
b) Instance Variables (Fields)
class Person
{
public string name; // Instance variable
}
Types of Variables
c) Static Variables
class Counter
{
public static int count = 0; // Static variable
}
d) Read-Only Variables
class Sample
{
public readonly int myValue;
public Sample(int value)
{
myValue = value;
}
}
Constants in C#
Syntax of Constant
const dataType constantName = value;
Example
const int MAX_USERS = 100; // Constant integer
Key Points about Constants
int a = 10, b = 5;
Console.WriteLine(a + b); // Output: 15
Console.WriteLine(a - b); // Output: 5
Relational (Comparison)
Operators
int a = 10, b = 5;
Console.WriteLine(a > b); // Output: True
Console.WriteLine(a == b); // Output: False
Logical Operators
Example
int a = 5, b = 3;
Console.WriteLine(a & b); // Output: 1
Console.WriteLine(a | b); // Output: 7
Input and Output (I/O) Operations
Example
Example
Console.Write("Press any key to continue...");
ConsoleKeyInfo key = Console.ReadKey();
Console.WriteLine("\nYou pressed: " +
key.KeyChar);
Input and Output (I/O) Operations
Value types
Value type variables can be assigned a value directly.
Reference types
The reference types do not contain the actual data stored in a
variable, but they contain a reference to the variables.
Pointer types
Pointer type variables store the memory address of another
type. Pointers in C# have the same capabilities as the pointers
in C or C++.
Data types
For Example :
Name : Zara Ali
Class : 6th
Section :J
Age : 13
Gender :F
Data types