Lecture 3
Lecture 3
By: DrSerat
Chapter 3
• Introduction to array
• Array declaration
• One dimensional array
Outline
• Two dimensional array
• Multi dimensional array
2
Introduction to array
3
Declaring array
oTo declare an array in C#, you can use the following syntax:
data type[] array name;
4
Initializing an Array
5
Assigning Values to an Array
o You can assign values to individual array elements, by using the index number, like:
int[] x = new int[4];
x[0] = 2;
o You can assign values to the array at the time of declaration as shown:
int[] x = { 1, 2, 3, 4};
You can also create and initialize an array as shown:
int [] x = new int[4] { 1, 2, 3, 4};
You may also omit the size of the array as shown:
int [] x = new int[] { 1, 2, 3, 4};
o You can copy an array variable into another target array variable. In such case, both the target and source
point to the same memory location:
int [] x = new int[] { 1, 2, 3, 4};
int[] y = x;
6
One dimensional array
using System;
namespace onedimension {
class Program {
static void Main(string[] args)
{
int[] a = { 1, 2, 3, 4 };
int[] b = new int[] { 1, 2, 3, 4 };
int[] c = new int[4] { 1, 2, 3, 4 };
int[] d;
d = new int[] { 1, 2, 3, 4 };
Console.WriteLine("a={0} b={1} c={2} d={3}",a[0], b[1], c[2], d[3]);
//now we want to display first array elements through for loop
for (int x = 0; x < 4; x++) {
Console.WriteLine("a={0}", a[x]); }
Console.ReadKey();
}}} 7
Two dimensional array
using System;
namespace twoDimension
{
class Program {
static void Main(string[] args) {
int[,] a = {{ 1, 2, 3, 4, 5, 6 }};
int[,] b = new int[,] {{1,2,3,4,5,6}};
int[,] c = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
int[,] d = new int[2, 3] {{ 1, 2, 3}, {4, 5, 6 }} ;
//Console.WriteLine("{0}", d[1, 0]);
//display through nested loop
for (int x = 0; x <2; x++) {
for (int y = 0; y < 3; y++) {
8
Console.WriteLine("{0}", d[x, y]); } }
Multi dimensional array
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication15
{
class Program {
static void Main(string[] args) {
int[, ,] a = {{ { 1, 2, 3, 4, 5, 6,7,8 }} };
int[, ,] b = new int[,,] { { { 1, 2, 3, 4, 5, 6,7,8 } } };
int[,,] c=new int[,,] {{ { 1,2},{3,4}},{ {5,6},{7,8}}};
int[,,] d=new int[2,2,2] {{ { 1,2},{3,4}},{ {5,6},{7,8}}};
//Console.WriteLine(c[0, 0, 0]);
//display through nested for loop
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
for (int z = 0; z < 2; z++) {
Console.WriteLine("{0}", c[x, y, z]); 9
Thanks
10
Chapter 2
11
Introduction to key basic syntax
Namespace declaration
Class
Class methods
Class attributes
Main method
Statements and Expressions
Comments
12
Introduction to key basic syntax
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" Hello seventh semester");
Console.ReadKey();
}
}
}
13
Introduction to key basic syntax
oThe first line of the program using System; the using keyword is
used to include the System namespace in the program. A program
generally has multiple using statements.
o The next line has a class declaration, the class Program contains the data and
method definitions that your program uses.
o Classes generally contain multiple methods. Methods define the behavior of the
class. However, the Program class has only one method Main.
o The next line defines the Main method, which is the entry point for all C#
programs.
o The Main method in C# is always declared with static because it can’t be call in
other method of function
16
Introduction to key basic syntax
using System;
namespace ConsoleApplication {
class Program {
static void Main() {
Console.WriteLine("Please enter any name");
} } }
17
Identifiers
o An identifier is a name used to identify a class, variable, function, or any other user defined item.
A name must begin with a letter that could be followed by a sequence of letters, digits (0 - 9) or
underscore.
19
C sharp keywords
o Keywords are reserved words predefined to the C# compiler. These keywords cannot be
used as identifiers. However, if you want to use these keywords as identifiers, you may
prefix the keyword with the @ character.
Contextual Keywords
20
Data Types
Value types
Reference types
Pointer types
21
Value Type
o Value type variables can be assigned a value directly.
They are derived from the class (System.ValueType).
22
Value Type
Type Represents Range
decimal 128-bit precise decimal values with 28-29 significant digits (-7.9 x 1028 to 7.9 x 1028) / 100 to 28
double 64-bit double-precision floating point type (+/-)5.0 x 10-324 to (+/-)1.7 x 10308
Float 32-bit single-precision floating point type -3.4 x 1038 to + 3.4 x 1038
23
Reference Type
o The reference types do not contain the actual data stored
in a variable, but they contain a reference to the variables.
o The object types can be assigned values of any other types, value
types, reference types, predefined or user-defined types.
However, before assigning values, it needs type conversion.
Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit.
26
Type conversion/ Example
27
Dynamic Type
o We can store any type of value in the dynamic data type variable.
o Type checking for these types of variables takes place at run-time.
o Syntax for declaring a dynamic type is:
dynamic <variable_name> = value; dynamic d = 20;
o Dynamic types are similar to object types except that type checking for object type
variables takes place at compile time, whereas that for the dynamic type variables takes
place at run time.
28
String Type
o String Type allows you to assign any string values to a variable. The string type is an alias for
the System.
o The value for a string type can be assigned using string literals in two forms: quoted and
@quoted. For example,
String str = “semester";
29
Pointer Type
o Pointer type variables store the memory address of another type.
30
Thanks
31
Lecture Agenda
• Visual Studio 2022 and Installation
• Create a Project in Visual Studio
• Class, Object and Object Initializer
• Primary Constructor
• What and why extension methods?
Visual Studio 2022 and
Installation
• Visual Studio is a comprehensive integrated development
environment (IDE) developed by Microsoft.
• It provides tools and services for building various types of
applications, including desktop, web, mobile, cloud, and more.
Visual Studio 2022 and
Installation
• To install Visual Studio 2022, you can visit the official Microsoft Visual
Studio website and download the installer.
• Run the installer and follow the on-screen instructions to complete
the installation process.
• During installation, you can choose the workload(s) relevant to your
development needs, such as .NET desktop development, ASP.NET web
development, or mobile development with .NET.
Create a Project in Visual Studio
• Open Visual Studio 2022.
• Click on "Create a new project" or go to File > New > Project.
• Choose the type of project you want to create (e.g., Console
Application, Windows Forms Application, ASP.NET Web Application,
etc.).
• Select the target framework and other project settings as needed.
Create a Project in Visual Studio
• Click on "Create" to create the project.
• Visual Studio will generate the necessary project files and structure
based on your selection.
Class, Object, and Object
Initializer
• In C#, a class is a blueprint for creating objects. It defines the
properties, methods, and behavior that objects of the class will have.
• An object is an instance of a class. It represents a specific instance of
the class with its own set of property values.
• Object initializer is a syntax in C# that allows you to initialize the
properties of an object at the time of object creation, rather than
setting them individually after the object has been created. It provides
a concise and readable way to initialize object properties.
Con…
• // Define a class
• public class Person
• {
• public string Name { get; set; }
• public int Age { get; set; }
• }
• // Create an object of the class and initialize its properties using object
initializer
• Person person = new Person { Name = "John", Age = 30 };
Primary Constructor
• A primary constructor is a feature introduced in C# 9.0 that allows you
to declare constructor parameters directly in the class's definition,
reducing boilerplate code.
• It simplifies the process of initializing object properties by
automatically assigning constructor parameters to class properties.
Con…
• // Define a class with a primary constructor
• public class Person(string name, int age)
•{
• public string Name { get; init; } = name;
• public int Age { get; init; } = age;
•}