C#

C#: 12-out & ref Keywords

out : Updating a Value type from called function

public static void ValueTypes()
{
    var x = 2;
    Console.WriteLine("ValueTypes: var x=2");
    Console.WriteLine("ValueTypes: Five(x): Before: {0}", x);
    // passes the VALUE to func so WILL NOT CHANGE ITS VALUE
    Console.WriteLine("ValueTypes: Five(x):calling..");
    Five(x);
    Console.WriteLine("ValueTypes: Five(x): AFTER: {0}", x);
}


 

static void Five(int a)
{
    a = 5;
    Console.WriteLine("ValueTypes: IN..Fix(int a): assigning a=5");
}


 


 

Tags

C#: 11-Value And Reference Types

Value type

int is value type

int x = 1;
int x;
int x = 0;
int x = null // throws compile error


 

Reference type

var employee = new employee();

 

Memory Considerations

using new, consumes memory

once variable is no longer used, the memory is still consumed, but no accessible

Memory Leaks occur when this happens

Garbage Collector will cleanup periodically, but this also consumes CPU power to manage

So writing lean code is important for larger or load-intensive applications

Stack

Memory management here is simpler

int x;

Heap ( with Garbage Collector )

More complex memory management

new Employee()
Tags

C#: 10-Enumerations/Enumerators

Given Original Code

Employee Class Field

public int Business; //index of the business name


 

Assignment

                    var emp = new Employee();

                    emp.Business = Util.EmployeeTrackerUtils.AskInt("Business Index Number: " +
                        "\n0:Apple \n1:Microsoft \n2:Samsung");
                    emp.Birthday = Util.EmployeeTrackerUtils.Ask("Employee Birthday: ");

Foreach

Tags

C#: 08-Exceptions

Exception catching must go from specific to generic

try{code}
catch{Specific}
catch{Generic}

Catching an Exception & Throwing a Message to Calling Method

Called method can catch an exception and 'throw new' message

        public static double AskDouble(string question)
        {
            try
            {
                System.Console.WriteLine(question);
                return double.Parse(Console.ReadLine());
            }
            catch(Exception)
            {
                // throws the error message up to calling methods
                throw new FormatException("Input was not a number");
            }
        }


 

Calling Try/Catch Block can receive the THROWN message

                catch (FormatException msg)
                {
                    Console.WriteLine(msg);
                }

Console

Tags

C#: 07-Parse and TryParse

int.Parse()

string.Parse()

double.Parse()

 

TryParse()

// Udpated for error handling on int.Parse
emp.Score = int.Parse(Util.EmployeeTrackerUtils.Ask("Employee Score: "));
int.TryParse(Util.EmployeeTrackerUtils.Ask("Employee Score: "), out emp.Score);
Tags

C#: 06-Collections

using System.Collections.Generic;

Lists

List<string> sList = new List<string>();
Tags

C#: 03-Console.WriteLine() & Console.ReadLine()

Console.WriteLine()

//Console.WriteLine()
Console.WriteLine("Hello World!");
Console.WriteLine("{0}","Hello World!");
Console.WriteLine("{0} {1}", "Hello", "World!");

 

Ouput

Hello World!
Hello World!
Hello World!

 

Console.ReadLine()

        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            string input = Console.ReadLine();
            Console.WriteLine("Input value = {0}", input);
        }

Output

Hello World!
MyInput
Input value = MyInput
Press any key to continue . . .
Tags
Subscribe to C#