0% found this document useful (0 votes)
19 views25 pages

05 C Basics Part II PDF

Uploaded by

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

05 C Basics Part II PDF

Uploaded by

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

CPP225

Object Oriented
Programming I

C# Basics
-
Part II

Öğr. Gör.
İhsan Can İÇİUYAN
2 Object Oriented Programming

Contents
 System Data Types

 Data Type Class Hierarchy

 Members of Data Types

 Parsing Values from String

 DateTime and TimeSpan

 BigInteger

 Digit Separators

 Binary Literals
3 Object Oriented Programming

System Data Types


 C# defines keywords for fundamental data types.

 C# data type keywords are actually shorthand notations for a real


type in the System namespace.
4 Object Oriented Programming

System Data Types

Table: Intrinsic Data Types of C#


5 Object Oriented Programming

System Data Types


 To declare a local variable → specify the data type, followed by
the variable’s name

 Consider the following example:


int myInt;
string myString;

 It is good practice to assign an initial value to your local data points


at the time of declaration:
int myInt = 0;
string myString;
myString = "This is my character data";

It is a compiler error to make use of a local variable


before assigning an initial value.
6 Object Oriented Programming

System Data Types


 You can also declare multiple variables of the same type on a
single line of code:

bool b1 = true, b2 = false, b3 = b1;

 It is also possible to allocate any data type using its full name:
System.Boolean b4 = false;

 EX: A program to illustrate various ways to declare a local variable.

 Code: 11_VarDeclarations.cs

Output:
Your data: 0, This is my character data, True, False, True, False
7 Object Oriented Programming

System Data Types


 default → assigns a variable the default value for its data type

 Consider the following example:


int myInt = default;

 You can also create a variable using the new keyword, which
automatically sets the variable to its default value:
 bool variables are set to false.
 Numeric data is set to 0 (or 0.0).
 char variables are set to a single empty character.
 BigInteger variables are set to 0.
 DateTime variables are set to 1/1/0001 12:00:00 AM.
 Object references (including strings) are set to null.
8 Object Oriented Programming

System Data Types


 Consider the following example:
bool b = new bool(); // Set to false
int i = new int(); // Set to 0
double d = new double(); // Set to 0.0
DateTime dt = new DateTime(); // Set to 1/1/0001 12:00:00 AM
9 Object Oriented Programming

Data Type Class Hierarchy


10 Object Oriented Programming

Data Type Class Hierarchy


 The data types are arranged in a class hierarchy.

 Types at the top of a class hierarchy provide some default


behaviors that are granted to the derived types.

 Each type ultimately derives from System.Object, which defines a


set of methods: ToString(), Equals(), GetHashCode()

 Many numerical data types derive from class System.ValueType.

 Descendants of ValueType → allocated on the stack

 Types that do not derive from ValueType → allocated on the


garbage-collected heap
11 Object Oriented Programming

Data Type Class Hierarchy


 Recall that a C# keyword for a data type is simply shorthand
notation for the corresponding system type.

 System data types eventually derives from System.Object and,


therefore, you can invoke any of its public members.

 Consider the following example:

Console.WriteLine("12.GetHashCode() = {0}", 12.GetHashCode());


Console.WriteLine("12.Equals(23) = {0}", 12.Equals(23));
Console.WriteLine("12.ToString() = {0}", 12.ToString());
Console.WriteLine("12.GetType() = {0}", 12.GetType());
12 Object Oriented Programming

Members of Data Types


 MaxValue and MinValue properties of the numerical types → provide
information regarding the range a given type can store

 System.Double type allows you to obtain the values for epsilon and
infinity.

 Consider the following example:


Console.WriteLine("Max of int: {0}", int.MaxValue);
Console.WriteLine("Min of int: {0}", int.MinValue);
Console.WriteLine("Max of double: {0}", double.MaxValue);
Console.WriteLine("Min of double: {0}", double.MinValue);
Console.WriteLine("double.Epsilon: {0}", double.Epsilon);
Console.WriteLine("double.PositiveInfinity: {0}",
double.PositiveInfinity);
Console.WriteLine("double.NegativeInfinity: {0}",
double.NegativeInfinity);
13 Object Oriented Programming

Members of Data Types


 Literal whole number (e.g. 500) → will default to an int

 Literal floating-point data (e.g. 55.333) → will default to a double

 To set the underlying data type to a


 long → use suffix l or L (e.g. 4L)
 float → use suffix f or F (e.g. 5.3F)
 decimal → use suffix m or M (e.g. 300.5M)

 Integral numeric types, Microsoft Learn: Link

 Floating-point numeric types, Microsoft Learn: Link


14 Object Oriented Programming

Members of Data Types


 System.Boolean data type provides TrueString and FalseString
properties, which yields the string "True" or "False", respectively.

 Consider the following example:

Console.WriteLine("bool.FalseString: {0}", bool.FalseString);


Console.WriteLine("bool.TrueString: {0}", bool.TrueString);

 C# textual data is represented by the string and char keywords,


both of which are Unicode under the hood.

 string → represents a contiguous set of characters (e.g., "Hello")

 char → represents a single slot in a string (e.g., 'H')


15 Object Oriented Programming

Members of Data Types


 System.Char data type provides character manipulation methods.

 Consider the following example:


char myChar = 'a';
Console.WriteLine("char.IsDigit('a'): {0}", char.IsDigit(myChar));
Console.WriteLine("char.IsLetter('a'): {0}", char.IsLetter(myChar));
Console.WriteLine("char.IsWhiteSpace('Hello There', 5): {0}",
char.IsWhiteSpace("Hello There", 5));
Console.WriteLine("char.IsWhiteSpace('Hello There', 6): {0}",
char.IsWhiteSpace("Hello There", 6));
Console.WriteLine("char.IsPunctuation('?'): {0}",
char.IsPunctuation('?'));

 NB: Many members of System.Char have two calling conventions:


 a single character
 a string with a numerical index that specifies the position of the
character to test
16 Object Oriented Programming

Parsing Values from String


 Parsing → the ability to generate a variable of their underlying type
given a textual equivalent

 You can use the static Parse() method to convert a string into the
underlying data type.

 Consider the following example:


bool b = bool.Parse("True");
Console.WriteLine("Value of b: {0}", b);
double d = double.Parse("99.884");
Console.WriteLine("Value of d: {0}", d);
int i = int.Parse("8");
Console.WriteLine("Value of i: {0}", i);
char c = char.Parse("w");
Console.WriteLine("Value of c: {0}", c);
17 Object Oriented Programming

Parsing Values from String


 An exception will be thrown if the string cannot be converted to the
correct data type.

 For example, the following will fail at runtime:


bool b = bool.Parse("Hello");

 To solve this issue, you can:


 wrap each call to Parse() in a try-catch block.
 use a TryParse() statement instead.

 TryParse() → takes an out parameter and returns a bool if the


parsing was successful
18 Object Oriented Programming

Parsing Values from String


 Consider the following example:
string value = "Hello";

if (double.TryParse(value, out double d))


{
Console.WriteLine("Success! Value of d: {0}", d);
}
else
{
Console.WriteLine("Failed! Default value of d: {0}", d);
}

If the value cannot be parsed, the variable is assigned


its default value, and the TryParse() method returns false.
19 Object Oriented Programming

DateTime and TimeSpan


 DateTime → contains data that represents a specific date (year,
month, day) and time value

 TimeSpan → allows you to define and transform units of time

 Consider the following example:

DateTime dt = new DateTime(2023, 11, 17);

Console.WriteLine("The day of {0} is {1}", dt.Date, dt.DayOfWeek);


dt = dt.AddMonths(1); // Month is now December
Console.WriteLine("Daylight savings: {0}", dt.IsDaylightSavingTime());

TimeSpan ts = new TimeSpan(4, 30, 0);


Console.WriteLine(ts);

Console.WriteLine(ts.Subtract(new TimeSpan(0, 15, 0)));


20 Object Oriented Programming

BigInteger
 BigInteger → to represent huge numerical values, which are not
constrained by a fixed upper or lower limit

 Defined in the System.Numerics namespace

 Establish the massive numerical value as a text literal and convert it


into a BigInteger variable via the static Parse() method.

BigInteger biggy =
BigInteger.Parse("9999999999999999999999999999999999999999999999");

 BigInteger class defines several static members that allow you to


apply basic mathematical expressions to BigInteger variables.
21 Object Oriented Programming

BigInteger
 Consider the following example:

BigInteger biggy =
BigInteger.Parse("9999999999999999999999999999999999999999999999");
Console.WriteLine("Value of biggy is {0}", biggy);
Console.WriteLine("Is biggy an even value?: {0}", biggy.IsEven);
Console.WriteLine("Is biggy a power of two?: {0}", biggy.IsPowerOfTwo);

BigInteger biggy2 = BigInteger.Multiply(biggy,


BigInteger.Parse("8888888888888888888888888888888888888888888"));
Console.WriteLine("Value of biggy2 is {0}", biggy2);

 You can also use mathematical operators, such as +, -, and *.

 Consider the following example:


BigInteger biggy3 = biggy * biggy2;
Console.WriteLine("Value of biggy3 is {0}", biggy3);
22 Object Oriented Programming

Digit Separators
 Sometimes when assigning large numbers to a numeric variable,
there are more digits than the eye can keep track of.
 e.g. 1234567, 24356378, 9245673.3467

 You can use the underscore (_) as a digit separator.


 e.g. 1_234_567, 24_356_378, 9_245_673.3467

Console.Write("Integer:");
Console.WriteLine(123_456);
Console.Write("Long:");
Console.WriteLine(123_456_789L);
Console.Write("Float:");
Console.WriteLine(123_456.1234F);
Console.Write("Double:");
Console.WriteLine(123_456.12);
Console.Write("Decimal:");
Console.WriteLine(123_456.12M);
Console.Write("Hex:");
Console.WriteLine(0x_00_00_FF);
23 Object Oriented Programming

Binary Literals
 Binary numbers can be written as follows:
0b_0001_0000

 Consider the following example:

Console.WriteLine("Sixteen: {0}", 0b_0001_0000);


Console.WriteLine("Thirty Two: {0}", 0b_0010_0000);
Console.WriteLine("Sixty Four: {0}", 0b_0100_0000);

 The output will be as follows:


Sixteen: 16
Thirty Two: 32
Sixty Four: 64
24 Object Oriented Programming

References
 Built-in types, https://learn.microsoft.com/en-us/dotnet/csharp/language-
reference/builtin-types/built-in-types

 The C# type system, https://learn.microsoft.com/en-


us/dotnet/csharp/fundamentals/types/

 Integral numeric types, https://learn.microsoft.com/en-


us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types

 Floating-point numeric types, https://learn.microsoft.com/en-


us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types

 DateTime Struct, https://learn.microsoft.com/en-us/dotnet/api/system.datetime

 TimeSpan Struct, https://learn.microsoft.com/en-us/dotnet/api/system.timespan

 BigInteger Struct, https://learn.microsoft.com/en-


us/dotnet/api/system.numerics.biginteger
25 Object Oriented Programming

That’s all for


today!

You might also like