using System; public abstract class Shape { private string myID; public Shape(string s) { id = s; } public string id { get { return myID; } set { myID = value; } } public abstract double Area { get; } public override string ToString() { return "ID: " + myID + string.Format("\t, Area: {0:F2} cm2",Area); } } public class Test { static void Main(string[] args) { Shape[] shapes = { new Square("Square #1", 10), new Circle("Circle #1", 10), new Rect("Rect #1", 20, 10), new Square("Square #2", 20), new Circle("Circle #2", 20), new Rect("Rect #2", 30, 20) }; foreach(Shape s in shapes) { Console.WriteLine(s); } } } public class Square : Shape { private int myLength; public Square(string id, int length) :base(id) { LENGTH = length; } public int LENGTH { get { return myLength; } set { myLength = value; } } public override double Area { get { return myLength * myLength; } } } public class Circle : Shape { private int myRadius; public Circle(string id, int rad) :base(id) { RAD = rad; } public int RAD { get { return myRadius; } set { myRadius = value; } } public override double Area { get { return System.Math.PI * myRadius * myRadius; } } } public class Rect : Shape { private int myLen; private int myWid; public Rect(string id, int len, int wid) :base(id) { LEN = len; WID = wid; } public int LEN { get { return myLen; } set { myLen = value; } } public int WID { get { return myWid; } set { myWid = value; } } public override double Area { get { return myLen * myWid; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test_2 { interface ICar { void beeb(); void getSpeed(); void getColor(); } class BMW : ICar { public void ssss() { } public void beeb() { Console.WriteLine("BMW = KHOSH BEEB ILEEHA"); } public void getSpeed() { Console.WriteLine("BMW = 300"); } public void getColor() { Console.WriteLine("BMW = White"); } } class KIA : ICar { public void beeb() { Console.WriteLine("KIA = MOZIEG BEEBHA HEEL"); } public void getSpeed() { Console.WriteLine("KIA = 150"); } public void getColor() { Console.WriteLine("KIA = Gray"); } } class Audi : ICar { public void beeb() { Console.WriteLine("Audi = QAOEY BEEBHA"); } public void getSpeed() { Console.WriteLine("Audi = 180"); } public void getColor() { Console.WriteLine("Audi = Black"); } } class TOYOTA : ICar { public void beeb() { Console.WriteLine("TOYOTA = BEEBHA HEEBA"); } public void getSpeed() { Console.WriteLine("TOYOTA = 200"); } public void getColor() { Console.WriteLine("TOYOTA = Blue"); } } class HYUNDAI : ICar { public void beeb() { Console.WriteLine("HYUNDAI = BEEBHA RAQY"); } public void getSpeed() { Console.WriteLine("HYUNDAI = 240"); } public void getColor() { Console.WriteLine("HYUNDAI = Yellow"); } } class Program { public static ICar Factory(int Fct) { if (Fct == 1) { return new BMW(); } else if (Fct == 2) { return new KIA(); } else if (Fct == 3) { return new Audi(); } else if (Fct == 4) { return new TOYOTA(); } else if (Fct == 5) { return new HYUNDAI(); } return null; } static void Main(string[] args) { int x = 11; if (x > 0 && x < 6) { ICar car = Factory(x); Console.WriteLine(car); if (car.GetType() == typeof(BMW)) { BMW dd = (BMW)car; dd.beeb(); dd.getColor(); dd.getSpeed(); } else if (car.GetType() == typeof(KIA)) { KIA dd = (KIA)car; dd.beeb(); dd.getColor(); dd.getSpeed(); } else if (car.GetType() == typeof(Audi)) { Audi dd = (Audi)car; dd.beeb(); dd.getColor(); dd.getSpeed(); } else if (car.GetType() == typeof(TOYOTA)) { TOYOTA dd = (TOYOTA)car; dd.beeb(); dd.getColor(); dd.getSpeed(); } else if (car.GetType() == typeof(HYUNDAI)) { HYUNDAI dd = (HYUNDAI)car; dd.beeb(); dd.getColor(); dd.getSpeed(); } } else { Console.WriteLine("THE Number Is Invalid"); } Console.ReadKey(); } } }
using System; class mainclass { public static void Main(string[] args) { int num01; int num02; Console.Write("type a number: "); num01 = Convert.ToInt32(Console.ReadLine() ); Console.Write("type a number: "); num02 = Convert.ToInt32(Console.ReadLine() ); Console.Write(+num01*num02); } }
using System.Collections.Generic; using System.IO; using System; class Program { //Criado para representar um caso de teste, criei uma classe para isso porque essa versão do c# não aceitou TUPLA private class CaseTest { //Lista para processamento public List<int> Lista {get; set;} //Valor esperado (correto) para uma caso especifico public int ExpectedResult{get; set;} } //Solucao -> O(n) private static int GetMaxSum (List<int> lista) { int firstLargestNumber = lista[0] > lista[1] ? lista[0] : lista[1]; int secondLargestNumber = lista[0] > lista[1] ? lista[1] : lista[0]; int listaLength = lista.Count; for (int i = 2; i < listaLength; i++) { //Caso o valor atual seja maior que o maior valor já processado if(lista[i] > firstLargestNumber){ //O antigo maior valor agora é o segundo secondLargestNumber = firstLargestNumber; //Atualizamos o maior valor firstLargestNumber = lista[i]; } //Caso o elemento atual seja maior que o segundo, mas não seja maior que o primeiro (a clausura If else if, garantira que apenas uma das 2 condições seja chamada) else if(lista[i] > secondLargestNumber) secondLargestNumber = lista[i]; } return secondLargestNumber + firstLargestNumber; } private static void unitTests() { // Casos de teste. Para testar funcionalidade inserir casos de teste List<CaseTest> caseTests = new List<CaseTest>() { new CaseTest {Lista = new List<int> () {1, 10, 3, 7, 9}, ExpectedResult = 19}, new CaseTest {Lista = new List<int> () {-2, 1, -1, 0}, ExpectedResult = 1}, new CaseTest {Lista = new List<int> () {-4, 20, 4}, ExpectedResult = 24}, new CaseTest {Lista = new List<int> () {-4, 2}, ExpectedResult = -2}, new CaseTest {Lista = new List<int> () {-51, 20, 31}, ExpectedResult = 51}, new CaseTest {Lista = new List<int> () {31, 20, 31}, ExpectedResult = 62}, new CaseTest {Lista = new List<int> () {12, 3, 45, 65, -70}, ExpectedResult = 110}, new CaseTest {Lista = new List<int> () {10, -15, 20, -30}, ExpectedResult = 30}, new CaseTest {Lista = new List<int> () {-20, 2, 10, 15}, ExpectedResult = 25}, new CaseTest {Lista = new List<int> () {0, 0}, ExpectedResult = 0}, }; foreach (CaseTest actualCaseTest in caseTests) { int calculatedValue = GetMaxSum(actualCaseTest.Lista); if(calculatedValue != actualCaseTest.ExpectedResult) Console.WriteLine("ERRO, Valor esperado : {0}, Valor calculado : {1} para lista : {2}", actualCaseTest.ExpectedResult, calculatedValue, string.Join(", ", actualCaseTest.Lista)); else Console.WriteLine("SUCCESS, Valor esperado : {0}, Valor calculado : {1} para lista : {2}", actualCaseTest.ExpectedResult, calculatedValue, string.Join(", ", actualCaseTest.Lista)); } } static void Main() { //Teste Unitarios unitTests(); } }
using System; namespace RectangleApplication { class Rectangle { //member variables private double length; private double width; public void Acceptdetails() { Console.WriteLine("Enter Length: "); length = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Enter Width: "); width = Convert.ToDouble(Console.ReadLine()); } public double GetArea() { return length * width; } public void Display() { Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); } }//end class Rectangle class ExecuteRectangle { static void Main(string[] args) { Rectangle r = new Rectangle(); r.Acceptdetails(); r.Display(); Console.ReadLine(); } } }
using System; using System.Threading; class Program { static Mutex mutexObj = new Mutex(); static int x=0; static void Main(string[] args) { for (int i = 1; i < 7; i++) { Thread myThread = new Thread(Count); myThread.Name = "Thread " + i.ToString(); myThread.Start(); } Console.ReadLine(); } public static void Count() { mutexObj.WaitOne(); x = 1; for (int i = 1; i < 3; i++) { Console.WriteLine("{0}: {1}", Thread.CurrentThread.Name, x); x++; Thread.Sleep(100); } mutexObj.ReleaseMutex(); } }
using System.IO; using System; using static System.Console; namespace LINQ { public class Book { public string Title{get;set;} public float Price{get;set;} } public class BookRepository { public IENumberable<Book> GetBooks() { return new List<Book> { new Book() {Title = "Book 1", Price = 1}, new Book() {Title = "Book2", Price = 2}, new Book() {Title = "Book3", Price = 3}, new Book() {Title = "Book4", Price = 4}, new Book() {Title = "Book4", Price = 5}, } } } class Program { static void Main(string[]args) { var Books = new BookRepository().GetBooks(); /*// TRADITIONAL METHOD var cheapBooks = new List<Book>(); foreach(var book in Books) { if(book.Price < 3) { cheapBooks.Add(book); } } foreach(var book in cheapBooks) { WriteLine("Book " + book.Title + "\nPrice = " + book.Price + "\n"); }*/ //LINQ Extension methods /*var cheapBooks = Books .Where(book = > book.Price > 3) .OrderBy(book => book.Title) .Select(book => book.Title);*/ //LINQ Query Operators /*var cheapBooks = from book in Books where book.Price < 3 orderby book.Title select book;*/ //var result = Books.FirstOrDefault(book => book.Title == "Book 4++"); //var result = Books.Last(book => book.Title == "Book 4++"); //var result = Books.LastOrDefault(book => book.Title == "Book 4++"); //var result = Books.Max(book => book.Price); //var result = Books.Min(book => book.Price); //var result = Books.Sum(book => book.Price); var result = Books.Skip(2).Take(3); //WriteLine("Average price = " + result); //WriteLine(result != null ? ("Title: " + result.Title + "\nPrice: " + result.Price + "\n") : "No book found"); foreach(var book in cheapBooks) { WriteLine("Book = " + book.Title + "\nPrice = " + book.Price + "\n"); //WriteLine("Book = " + book.Title); } } } }
//Click "Execute" //Then answer each Question on the STDIN using System.IO; using System; class Program { static void Main() { Console.WriteLine("Hello, world."); Console.WriteLine("Can you tell me your name?"); string usersName = ""; usersName = Console.ReadLine(); var Helloline = "Hi, " + usersName; Console.WriteLine(Helloline); Console.WriteLine("How are you doing?"); Console.WriteLine("The options are Good or Bad."); var feeling1 = ""; feeling1 = Console.ReadLine(); if (feeling1 == "Good" || feeling1 == "good") { Console.WriteLine("Great!"); } if (feeling1 == "Bad" || feeling1 == "bad") { Console.WriteLine("Walk it off," + usersName + "."); } else { Console.WriteLine("This is not a valid option."); } Console.WriteLine("Are you a boy or a girl?"); string gender = ""; gender = Console.ReadLine(); if (gender == "Boy" || gender == "Girl"||gender == "girl"||gender == "boy") { if (gender == "Boy"||gender == "boy") { Console.WriteLine("Great!"); } else { Console.WriteLine("Cool!"); } } else { Console.WriteLine("This is not a valid option"); } Console.WriteLine("How many friends do you have?"); Console.WriteLine("There are the options 'A lot' or 'Not many'"); var friends = ""; friends = Console.ReadLine(); if (friends == "a lot" || friends == "not many"||friends == "A Lot"||friends == "Not Many"||friends == "Not many"||friends == "A lot"||friends == "a Lot"||friends == "not Many") { if (friends == "Not many"||friends == "Not Many"||friends == "not Many"||friends == "not many") { Console.WriteLine("Oh, I'm sorry!"); } else { Console.WriteLine("Same!"); } } else { Console.WriteLine("This is not a valid option."); } Console.WriteLine("How many fights have you gotten into?"); Console.WriteLine("The options are 'less than 100' and 'more than 100'"); var fights = ""; fights = Console.ReadLine(); if (fights == "less than 100" || fights == "more than 100"|| fights == "More than 100"|| fights == "More Than 100"|| fights == "more Than 100"||fights == "Less than 100"||fights == "Less Than 100"||fights == "less Than 100") { if (fights == "more than 100"|| fights == "More than 100"|| fights == "More Than 100"|| fights == "more Than 100") { Console.WriteLine("Same here!"); } else { Console.WriteLine("Yeah, totally."); } } else { Console.WriteLine("This is not a valid option."); } Console.WriteLine("I'm a little nosy, aren't I?"); Console.WriteLine("The options are 'No' and 'No'"); var opinion = ""; opinion = Console.ReadLine(); if (opinion == "Yes" || opinion == "No"||opinion == "yes"||opinion == "no") { if (opinion == "Yes"|| opinion == "yes") { Console.WriteLine("Cut me some slack!"); Console.WriteLine("You have found the secret ending."); } else { Console.WriteLine("I knew it!"); } } else { Console.WriteLine("This is not a valid option."); } Console.WriteLine("This is the end?"); Console.ReadLine(); } }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more