Skip to content

Instantly share code, notes, and snippets.

@amysimmons
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save amysimmons/0edd4c209635a37efabe to your computer and use it in GitHub Desktop.

Select an option

Save amysimmons/0edd4c209635a37efabe to your computer and use it in GitHub Desktop.
C# Fundamentals

#C# Fundamentals with C# 5.0

##Introduction to C#

To create a new project in Visual Studio:

File, new, Visual C# , Console Application

In the command prompt, cd to the project folder, obj, debugg, and run the .exe file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sleep
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("Please enter your name");
			string name = Console.ReadLine();

			Console.WriteLine("How many hours of sleep did you get last night?");
			int hoursOfSleep = int.Parse(Console.ReadLine());

			Console.WriteLine("Hello, " + name);

			if (hoursOfSleep < 8)
			{
				Console.WriteLine("You must be tired, " + name);
			}
			else
			{
				Console.WriteLine("You seem well rested, " + name);
			}
		}
	}
}

##Classes and Objects in C#

We use classes to create abstractions for the different nouns that we work with.

Example task: We need an electronic gradebook to read the scores of an individual student and then compute some simple stats for the scores.

What class should we write?

It would make sense to start with a class of Gradebook to implement some of this functionality.

###The initial steps in Visual Studio:

  • File, new project, Visual C#, Console Application

  • Name it 'Grades'

  • Visual Studio automatically creates a Program class (Program.cs).

  • Now we want to add another class.

  • Right clik the project, add class, and call it Gradebook.

  • Because I named the file Gradebook, VS created a Gradebook class and put it in a namespace called Grades, the name of our project.

###Class Members

Anythin you add to the class will be a member of that class.

Class Members Define: 1. State; 2. Behaviour

  • Start by adding a Member that will allow me to add a grade to the gradeook.
using System;
using System.Collections.Generic;
namespace Grades
{
	class Gradebook
	{
		public void AddGrade(float grade)
		{
			//adds to the grades collection the grade that is passed into the method 
			grades.Add(grade)
		}	
	
		//this is a class member that represents state 
		//it creates a list of floating point numbers
		//this line declares a field in my program 
		List<float> grades = new List<float>();
	}
}

###The new constructor

When I create a new class defiition I am creating a new type.

When I create a new type I can use the type to declare variables, parameters to methods, etc.

But there is another important aspect of declaring a class.

I can use the class to instantiate one or more objects using that class.

It is objects that I need to work with in my program. Objects hold the data I need and are allocated into the memory of the computer.

There is nothing useful I can do with the Gradebook class, until I use it to create objects.

For example, I can declare a variable called book, but this variable needs to reference an object.

Classes define a type: Use the types for variables and arguments

Classes create objects: Invoke methods and save state

The new keyword creates a new instance of Gradebook, which can be assigned into a variable.

new is a constructor method, which is used to create a new object, or an instance of the class.

This means my variable is no longer uninitialized.

namespace Grades
{
	class Program 
	{
		static void Main(string[] args)
		{
			//I create a variable called book with a type of Gradebook here because from inside of this Program 
			//class I can use any other class that I have defined in this same project,
			//as well as types from the framework class library
			//new is a constructor method to create a new object, which is an instance of the class
			GradeBook book = new GradeBook();
			
			//AddGrade is available to be called on book because I have it defined as a membe/method  of my class 
			book.AddGrade(91f);
			book.AddGrade(89.5f);
		}
	}
}

At this point, if I go into the debugger (f10), step over the lines (f10), and hover over 'book', it should have a count of 2.

The difference between float and double in C#:

???

###Constructors

When I use the new keyword and the name of a class with parentheses this is instantiating an instance of a class, or creating an object.

Every class has a default constructor that exists implicitly, unless I do something special. This is why I can create an instance of GradeBook even though I don't have a constructor explicitly defined.

GradeBook book = new GradeBook();

If I do define a constructor, it's very much like defining a method.

public GradeBook()
{
   //initialization code 
}

When you want to take control over the initiailization of your object, that's when you'll want to write a constructor.

###Objects and variables

###Reference Types

Classes are reference types

Variables point to objects. Variables don't hold objects in their storage location.

I can have multiple variables, all of the same type, and all pointing to the exact same object, eg:

GradeBook book2 = book1;

###OOP

  • objects are nouns
  • methods are verbs
  • objects encapsulate functionality

###Encapsulation

###Access Modifiers: Public and private

Access modifiers are keywords like public and private that describes who is allowed to access and use certain fields or methods.

Public and private are just two of the different access modifiers.

private means a method or field can only be used from inside of the class, while public means it can be used anywhere.

when you add members to a class you wil need to think about whether you want them to be public, or private: is this an implementation detail that i can hide? or is this something that i need to make publicly available os that other people can do interesting work with it?

###Statics

Statics use static members of a class without creating an instance.

public static float MinimumGrade = 0;
public static float MaximumGrade = 100;

The above fields are static fields. What's important is that a static field or method on a class is something we can access without creating an instance of a class.

Console.WriteLine("Hello!");
Console.WriteLine(GradeBook.MaximumGrade);

So GradeBook.MaximumGrade is an expression that returns 100, and I don't need to create an instance of the class to use it.

This is also what Console.WriteLine is doing.

###Computation

##Types and Assemblies

##Members: Methods, Events and Properties

##Flow Control

##Object Oriented Programming

http://www.pluralsight.com/courses/csharp-fundamentals-csharp5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment