Skip to content

Instantly share code, notes, and snippets.

@LeilaniL
Created September 29, 2020 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeilaniL/7a8a61cff8aef6d6356285f67248d59e to your computer and use it in GitHub Desktop.
Save LeilaniL/7a8a61cff8aef6d6356285f67248d59e to your computer and use it in GitHub Desktop.
Example of a class in C#
using System;
using System.Collections.Generic;
namespace ObjectsDemo
{
class Movie
{
public string Name { get; set; }
public string Director { get; set; }
public string Genre { get; private set; }
public bool Seen { get; set; }
public int Rating { get; set; }
private static List<Movie> Favorites = new List<Movie> { };
public Movie(string movieName)
{
Name = movieName;
Director = "";
Genre = "Drama";
Seen = false;
Rating = 0;
}
public override string ToString()
{
if (Director != "" && Rating != 0)
{
return Director + "'s " + Name + ": " + Rating + " stars";
}
else if (Director != "")
{
return Director + "'s " + Name;
}
else if (Rating != 0)
{
return Name + ": " + Rating + " stars";
}
else
{
return Name;
}
}
public void RateMovie(string rating)
{
Rating = int.Parse(rating);
Seen = true;
if (Rating >= 4)
{
Favorites.Add(this);
}
Console.WriteLine("{0} has a rating of {1}", Name, Rating);
}
public void SetGenre(string movieType)
{
string typeNoSpaces = movieType.Trim();
var genreList = new List<string> { "drama", "comedy", "scifi", "fantasy", "romance", "romcom" };
if (genreList.Contains(typeNoSpaces))
{
Genre = typeNoSpaces;
Console.WriteLine("{0} is a {1}", Name, Genre);
//console.log("instance of class: ", object, object.county)
}
else
{
Console.WriteLine("??? Unknown genre");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment