Skip to content

Instantly share code, notes, and snippets.

@harshityadav95
Created July 7, 2017 19:52
Show Gist options
  • Save harshityadav95/0f3e1bf7e3ff05f7f39060b60c304a2d to your computer and use it in GitHub Desktop.
Save harshityadav95/0f3e1bf7e3ff05f7f39060b60c304a2d to your computer and use it in GitHub Desktop.
C# classes and Structures
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
namespace After001
{
internal class Program
{
private static void Main(string[] args)
{
var man = new Man("John", "Doe");
System.Console.WriteLine("{0} {1}", man.FirstName, man.LastName);
man.Update("Jane", "Doe");
System.Console.WriteLine("{0} {1}", man.FirstName, man.LastName);
man.Update(lastName: "Smith", firstName: "Bob");
System.Console.WriteLine("{0} {1}", man.FirstName, man.LastName);
man.Update("Mark");
System.Console.WriteLine("{0} {1}", man.FirstName, man.LastName);
System.Console.Read();
}
void List()
{
var objects = new ArrayList();
objects.Add(1);
objects.Add(2);
objects.Add("three");
var list = new List<int>();
list.Add(1);
list.Add(2);
list.Add("three");
}
// GENERICS
void Breakfast()
{
var bird = new Animal<Egg>();
var pig = new Animal<Piglet>();
}
public class Animal<T> where T : Offspring
{
public T Offspring { get; set; }
}
public abstract class Offspring { }
public class Egg : Offspring { }
public class Piglet : Offspring { }
public class Man
{
public Man(string firstName, string lastName)
{
Update(firstName, lastName);
}
public void Update(string firstName, string lastName = "Jones")
{
this.FirstName = firstName;
this.LastName = lastName;
}
private string _firstName;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
private string _lastName;
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment