0% found this document useful (0 votes)
12 views10 pages

C# Properties and Data Types Explained

Uploaded by

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

C# Properties and Data Types Explained

Uploaded by

Ravi Bhalerao
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROPERTY EXAMPLES [get{} & set{}]

Code:

using System;
using [Link];
using [Link];
using [Link];

namespace ConsoleApplication4
{
class example
{
int _number;
public int number
{
get
{
return this._number;
}
set
{
this._number = value;
}
}
}
class Program
{
static void Main(string[] args)
{
example example = new example();
[Link] = 5; //set{}
[Link]("Number is " + [Link]); //get{}
[Link]();
}
}
}
Output
Read-Only Property

Code:

using System;
using [Link];
using [Link];
using [Link];

namespace ReadOnlyProperty
{
class abc
{
int sal = 100;
int tax = 5;
public int MySalary
{
get
{
return sal;
}
}
public int MyTax
{
get
{
return tax;
}
}
}
class Program
{
static void Main(string[] args)
{
abc obj = new abc();
int sat = [Link] - [Link];
[Link]("Salary After Tax "+sat);
[Link]();
}
}
}
Output:
Write-Only Property

using System;
using [Link];
using [Link];
using [Link];

namespace ReadOnlyProperty
{
class abc
{
int sal, tax;
public int mytax
{
set
{
tax = value;
}
}

public int mysal


{
set
{
sal = value;
}
}

public void display()


{
int sat = sal - tax;
[Link]("Salary after tax "+sat);
[Link]();
}

class Program
{
static void Main(string[] args)
{
abc obj = new abc();
[Link] = 100;
[Link] = 5;
[Link]();
}
}
}
Now let's start from the beginning:
using System;
using [Link];
using [Link];

namespace is a collection of classes

Classes brings us some sort of functionality

class is simply called Program

class Program

A class can contain several variables, properties and methods

Data types
Data types are used everywhere in a programming language like C#. Because it's a strongly typed language, you
are required to inform the compiler about which data types you wish to use every time you declare a variable, as
you will see in the chapter about variables. In this chapter we will take a look at some of the most used data types
and how they work.

bool It can contain only 2 values - false or true. The bool type is important to understand when using logical
operators like the if statement.

int is short for integer, a data type for storing numbers without decimals. When working with numbers, int is the
most commonly used data type. Integers have several data types within C#, depending on the size of the number
they are supposed to store.

string is used for storing text, that is, a number of chars. In C#, strings are immutable, which means that strings
are never changed after they have been created. When using methods which changes a string, the actual string is
not changed - a new string is returned instead.

char is used for storing a single character.

float is one of the data types used to store numbers which may or may not contain decimals.
Variables
A variable can be compared to a storage room

<data type> <name>;

It can be declare like this

<visibility> <data type> <name> = <value>;

private string name = "John Doe";


[Link]()

It simply reads a string and converts it into an integer.

Loops
The while loop
while(number < 5)
{
[Link](number);
number = number + 1;
}

The do While loop


do
{
[Link](number);
number = number + 1;
} while(number < 5);

The for loop


int number = 5;

for(int i = 0; i < number; i++)


[Link](i);

[Link]();
The foreach loop
using System;
using [Link];

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
[Link]("John Doe");
[Link]("Jane Doe");
[Link]("Someone Else");

foreach(string name in list)


[Link](name);

[Link]();
}
}
}
Functions
<visibility> <return type> <name>(<parameters>)

<function code>

public int AddNumbers(int number1, int number2)


{
int result = number1 + number2;
return result;
}

Function parameters
two parameters: "by value" and "by reference".

The default in C# is "by value"

WRONG Code

static void Main(string[] args)


{
int number = 20;
AddFive(number);
[Link](number);
[Link]();
}

static void AddFive(int number)


{
number = number + 5;
}

Output: 20
Why ? = The value we assign to number inside the function, is never carried out of the function, because we have
passed a copy of the number value instead of a reference to it.
Right Code

static void Main(string[] args)


{
int number = 20;
AddFive(ref number);
[Link](number);
[Link]();
}

static void AddFive(ref int number)


{
number = number + 5;
}
Output: 25

The params modifier


Functions with params can even take other parameters as well, as long as the parameter with the params
keyword are the last one. Besides that, only one parameter using the params keyword can be used per function.

class Program
{
static void Main(string[] args)
{
Greetpersons(0);
Greetpersons(25, "Ravi", "Dattatray", "Bhalerao");
[Link]();
}

static void Greetpersons(int someunusedparam,params string[] names)


{
foreach (string name in names)
[Link]("Hello" + name);
}
}
Arrays
Arrays works as collections of items

Arrays are declared much like variables, with a set of [] brackets after the datatype

string[] names;

You need to instantiate the array to use it

string[] names = new string[2];

e.g

class Program
{
static void Main(string[] args)
{
string[] names = new string[2];

names[0] = "John Doe";


names[1] = "Jane Doe";

foreach(string s in names)
[Link](s);

[Link]();
}

You might also like