C datatypes
This presentation is the property of its rightful owner.
Sponsored Links
1 / 40

C# Datatypes PowerPoint PPT Presentation


  • 111 Views
  • Uploaded on
  • Presentation posted in: General

C# Datatypes. CNS 3260 C# .NET Software Development. Types. Simple types int, uint, float, etc. keywords represent structs in IL Complex types Classes Structs Enums Delegates Everything inherits from System.Object. C# Value vs. Reference.

Download Presentation

C# Datatypes

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


C datatypes

C# Datatypes

CNS 3260

C# .NET Software Development


Types

Types

  • Simple types

    • int, uint, float, etc.

    • keywords represent structs in IL

  • Complex types

    • Classes

    • Structs

    • Enums

    • Delegates

  • Everything inherits from System.Object


C value vs reference

C# Value vs. Reference

  • Value Types “directly contain” the variable data.

    • Example: int, float, enum, struct...

  • Reference Types contain a reference to the data

Reference Type

Value Type

myObject


Class valuetype

class ValueType

  • ValueType is a class that inherits directly from object

    • You cannot inherit from ValueType

  • ValueType overrides System.Object methods so they make sense


Value types

value-types

  • value-types inherit directly from ValueType

    • simple types

    • enum types

    • struct types

  • All value-types are sealed

  • value-types cannot be null

  • Assignment of a value-type results in a copy of the variable


Value type hierarchy

value-type Hierarchy

value-type

struct-type

enum-type

simple-type

type-name

type-name

numeric-type

bool

integral-type

floating-point-type

decimal


Simple types

Simple Types

integral-types

Signed

Unsigned

Unicode


Simple types continued

Simple Types (Continued)

floating-point-types

Decimal

Boolean


Numeric type methods

numeric-type Methods

  • static MinValue

  • static MaxValue

  • static Parse

    • static Parse(string)

    • static Parse(string, NumberStyles)

  • ToString

    • ToString()

    • ToString(string format)

int index = 256;

index.ToString(“X”);


System char remember it s 16 bit

System.Char (remember it’s 16-bit)

  • Has ingetral-type methods, plus...

  • static IsDigit()

  • static IsLetter()

  • static IsDigitOrLetter()

  • static IsLower()

  • static IsNumber()

  • static IsPunctuation()

  • static IsUpper()

  • static ToUpper()

  • static ToLower()

  • and more...


Floating point types

floating-point-types

  • Have numeric-type methods, plus...

  • static Epsilon

  • static NaN

  • static NegativeInfinity

  • static PositiveInfinity

  • static IsInfinity()

  • static IsNaN()

  • static TryParse()

  • and more...


System decimal decimal

System.Decimal (decimal)

  • Have numeric-type methods, plus...

  • static Floor

  • static GetBits()

  • static ToDouble()

  • static ToSingle()

  • static ToInt64()

  • static Round()

  • static Truncate()

  • and more...


System boolean bool

System.Boolean (bool)

  • Have numeric-type methods, plus...

  • static FalseString

  • static TrueString

  • and (not much) more...


Structs and enums

structs and enums

  • An enumeration type is a distinct type with named constants.

  • Every enumeration type has an underlying type, which must be byte, sbyte, short, ushort, int, uint, long or ulong.

  • A struct type is a value type that can declare constants, fields, methods, properties, indexers, operators, instance constructors, static constructors, and nested types.

  • We’ll cover structs and enums later


Reference types

Reference Types

  • Classes

    • object (System.Object)

    • string

    • user-defined classes

  • Interfaces

  • Arrays

  • Delegates

    • delegate (System.Delegate)


Reference type hierarchy

reference-type Hierarchy

reference-type

class-type

interface-type

array-type

delegate-type

type-name

object

string


String type

String Type

String Type (Immutable reference type)


System string string

System.String (string)

  • static Empty

  • StartsWith(string)

  • EndsWith(string)

  • IndexOf(string)

  • static Join(string, string[])

  • Split(char[], int)

  • Remove(int, int)

  • Replace(string, string)

  • Insert(int, string)

  • and much more...


Object

Object

  • Four virtual methods:

    • Equals(object obj)

    • GetHashCode()

    • GetType()

    • ToString()

  • Two static methods:

    • static Equals(object objA, object objB)

    • static ReferenceEquals(object objA, objB)

  • One protected method:

    • Finalize()


Object virtual methods

Object Virtual Methods

  • Equals(object obj)

    • Reference types: the default supports reference-equals

    • Value types: the default supports bitwise equality.

    • Can be overridden

  • GetHashCode()

    • Returns a Hash value

  • GetType()

    • Returns the exact runtime type of the calling instance

  • ToString()

    • Returns a string that represents the calling instance


Object static methods

Object Static Methods

  • Equals(object objA, object objB)

    • This method first determines whether both parameters are null references before calling objA.Equals(objB)

  • ReferenceEquals(object objA, object objB)

    • Returns true if objA is the same instance as objB or if both are null references; otherwise, false


Try this

Try this:

class Class1

{

[STAThread]

static void Main(string[] args)

{

Console.WriteLine(object.Equals(null, null));

Console.WriteLine(object.ReferenceEquals(null, null));

Console.ReadLine();

}

}

Q: What’s the difference between Equals and ReferenceEquals?

A: Equals calls objA.Equals(objB). So if objA redefined .Equals(), that

overridden method will be invoked.

ReferenceEquals will always tell if the references are the same.


Everything derives from object

Everything derives from object

  • 3.ToString();

  • “hello”.GetHashCode();

  • 5.Equals(5);

  • myObject.Equals(yourObject);


Boxing

Boxing

  • Value Types which are up-cast to a reference type are boxed

    • This is necessary because Collections hold objects

  • Boxing places a copy of the variable in the object

  • Extra information (such as type info) may be stored with the variable, the standard is not explicit


Boxing example

Boxing example

class Test

{

static void Main()

{

int i = 123;

object o = i; // boxing

int j = (int) o; // unboxing

}

}


Type conversions

Type Conversions

Implicit Conversions

Implicit conversions will occur when there is no loss of information.

Example:

using System;

class Test

{

static void Main()

{

int intValue = 123;

long longValue = intValue;

Console.WriteLine("{0}, {1}", intValue, longValue);

}

}


Conversions

Conversions

Explicit Conversions

Explicit conversions are required when there could be loss of information.

Example:

using System;

class Test

{

static void Main()

{

long longValue = System.Int64.MaxValue;

int intValue = (int)longValue;

Console.WriteLine(“(int){0} = {1}", longValue, intValue);

}

}


Conversions1

Conversions

Explicit or Implicit?

Explicit

float to Int32?

Implicit

Int32 to float?

Explicit

decimal to float?

Implicit

long to decimal?

Explicit

Int32 to UInt32?

Explicit

UInt32 to Int32?

(See Conversions Demo)


Local variables

Local variables

  • must be assigned before they are used

  • no static locals

  • no const locals

Example:

void fun(){

int a, b = 3;

int c = a + b;

}


Fields

Fields

  • static

  • instance

public class Rectangle{

public int Top = 0;

public int Left = 0;

public int Height = 100;

public int Width = 100;

private static int RectangleCount;

public Polygon(){

++ RectangleCount;}

public ~Polygon(){

-- RectangleCount;}

}


Local vs field

Local vs. Field

class SomeClass{

private int index;

public SomeClass(int index){index = index; // WRONG!!!

this.index = index;

}

}


Pointers

Pointers?

  • only in unsafe blocks (we’ll explore these later.)


Passing parameters value

Passing Parameters: Value

class MainClass

{

static void Main()

{

int i = 0;

new MainClass().fun(i);

Console.WriteLine(i.ToString());

Console.ReadLine();

}

private void fun(int x)

{

x = 5;

}

}

What does it mean to pass a reference by value?

(See PassReference Demo)


Passing references

Passing references

  • Passing object to a method only passes reference

    • Fast

    • Easy

    • Somewhat misunderstood

    • No implicit deep copy if needed


Passing parameters ref

Passing Parameters: ref

class MainClass

{

static void Main()

{

int i = 0;

new MainClass().fun(ref i);

Console.WriteLine(i.ToString());

Console.ReadLine();

}

private void fun(ref int x)

{

int y = x; // ok

x = 5;

int z = x; // ok

}

}

If a reference type is being passed, then it is a reference to the reference.


Passing parameters out

Passing Parameters: out

Similar to passing by ref, but the value is initially unassigned.

Also, you must assign to the parameter somewhere in the method.

class MainClass

{

static void Main()

{

int i = 0;

new MainClass().fun(out i);

Console.WriteLine(i.ToString());

Console.ReadLine();

}

private void fun(out int x)

{

// int y = x; // not ok

x = 5;

int z = x; // ok

}

}


Variable length arguments

Variable Length Arguments

Uses params keyword.

class Test

{

static void F(params int[] args)

{

Console.WriteLine("# of arguments: {0}", args.Length);

for (int i = 0; i < args.Length; i++)

Console.WriteLine("\targs[{0}] = {1}", i, args[i]);

}

static void Main()

{

F();

F(1);

F(1, 2);

F(1, 2, 3);

F(new int[] {1, 2, 3, 4});

}

}


Intro to delegates

Intro to delegates

  • Think of a delegate as a function pointer that stores a reference to its instance.

C++ function pointer:

int* (*funPtr)(char arg1, float arg2);

C# delegate:

public delegate void funDelegate(char arg1, float arg2);


Delegate syntax

Delegate Syntax

  • Delegate declaration declares a new type.

public delegate string FunDelegate(int a, float b);

public class MainClass

{

[STAThread]

static void Main()

{

FunDelegate fd = new FunDelegate(fun);

fd(5, 3.14159);

}

private string fun(int x, float f)

{

return (x+f).ToString();

}

}


Delegates

Delegates

  • Why use delegates (or function pointers?)

Keeps coupling low

Makes code more extensible

Asynchronous processing

(See SimpleDelegateDemo)


  • Login