Programming C#

Tasks studies - laboratory


Project maintained by dawidolko Hosted on GitHub Pages — Theme by dawidolko

Object-oriented programming – C#

INTERFACES

An interface is a structure similar to a class, with the difference that it only has declarations of member methods and does not implement them. You cannot create an object that will be an instance of an interface. A class, on the other hand, can implement an interface. A class implementing an interface must have implementations of all interface components. This means that the interface tells the class what to include (what methods and properties). Importantly, a class can inherit only from one class, but it can implement multiple interfaces at once. An interface can be an extension interface for many other interfaces, but no extension interface can be more accessible than the base interface. A class implementing an interface can be more accessible than the interface.

interface access modifier IInterfaceName [: baselist]
{
interface components;

}
By default, all interface component modifiers are public. public interface IHasCreationTime
 {
 DateTimeCreationTime
 {
 get;
 set;
 }
 }
 public interface IDataRepository
 {
 List<int>GetData();
}

Extension interface:

public interface ICreationAudited : IHasCreationTime
{
 int? CreatorUserId
 {
 get;
 set;
 }
}

A class can inherit from multiple interfaces

public class Ticket : IHasCreationTime, IHasModificationTime, IHasDeletionTime
{
 public DateTime CreationTime { get; set; }
 public DateTime? DeletionTime { get; set; }
 public bool IsDeleted { get; set; }
 public DateTime? LastModificationTime { get; set; }
}

Usage:

IHasCreationTime item = new IHasCreationTime(); //ERROR
IHasCreationTime item2 = new FilmShow();

An interface cannot contain:

• constants,

• fields,

• operators,

• constructors and destructors,

• nested types.

Most popular use of interfaces. Encapsulation of classes and universal methods

public class Ticket : ICreationAudited
{
  public int? CreatorUserId
  {
    get;
    set;
  }
  public DateTime CreationTime
  {
    get;
    set;
  }
  public string Name { get; set; }
  }
  public class FilmShow : ICreationAudited
    {
    public int? CreatorUserId
    {
      get;
      set;
  }
  public DateTime CreationTime
  {
    get;
    set;
    }
  }
  public static void SetCreationUser(ICreationAudited creationAudited)
  {
creationAudited.CreatorUserId = Session.GetUserId();
}

WykorzystaUsenie

var filmShow = new FilmShow();
SetCreationUser(filmShow);
var ticket = new Ticket();
SetCreationUser(ticket);

Repozytoria – pobieranie danych

public interface ITicketsRepository
{
  List<Ticket> GetAllTickets();
  void AddNewTicket(Ticket item);
  }
  public class TicketsRepository : ITicketsRepository
  {
  public void AddNewTicket(Ticket item)
  {
  //instrukcje dodawania do bazy
  }
  public List<Ticket> GetAllTickets()
  {
    //instrukcja pobierania z bazy danych
    throw new NotImplementedException();
  }
}

Testowy interfejs:

public class MockTicketsRepository : ITicketsRepository
{
  private List<Ticket> list;
  public MockTicketsRepository()
  {
    list = new List<Ticket>()
  {
    new Ticket() { Name = "Item1" },
    new Ticket() { Name = "Item2" },
    new Ticket() { Name = "Item3" }
  };
  }
  public void AddNewTicket(Ticket item)
  {
    list.Add(item);
  }
  public List<Ticket> GetAllTickets()
  {
    return list;
  }
}

Use:

ITicketsRepository ticketsRepository;
#if DEBUG
ticketsRepository = new MockTicketsRepository();
#else
ticketsRepository = new TicketsRepository();
#endif
var ticketsList = ticketsRepository.GetAllTickets();
var ticket = new Ticket();
SetCreationUser(ticket);
ticketsRepository.AddNewTicket(ticket);

Tasks to do yourself:

1)

Define the IOsoba interface. It should require the implementation of the FirstName, LastName properties and the ReturnFullName method. Then create a Person class that inherits from this interface and implements it. Create several instances of this class, add them to the List<Iospoba> list.

2)

Write a method extending List<Iospoba> void WypiszOsoby(), and then implement it.

(printing the first and last name of people to the console)

3)

Write a method extending List<Iospoba> void PosortujOsobyByNazive(), and then implement it. ### 4) Write an IStudent interface that extends the IOsoba interface with the University, Direction, Year, Semester properties. Then create a Student class that implements the IStudent interface and contains an additional method: string WypiszPelnaNazweIUczelnie(), which will print out full information about the student, e.g.: Jan Kowalski - 2INF 2021 UR

5)

Write and implement a StudentUR class that inherits from the Student class. Create several instances of this class, add them to the list, and then overload the method from task 2 to allow you to use the method WypiszPelnaNazweIUczelnie().