C# course 2

  1. Funkcje w C# – weird syntax, but works:
 //funkcja generuje 30 liczb losowych
    public static int[] ScanTest()
    {
        var answers = new int[30];

        Random random = new Random();

        for (int i = 0; i < answers.Length; i++)
        {
            answers[i] = random.Next(0, 2);
        }

        return answers;
    }

    //funkcja zliczajaca sume wszystkich skladnikow tablicy
    public static double CalculateScore(int[] answers)
    {
        int sum = 0;

        for (int i=0;i<answers.Length;i++)
        {
            sum += answers[i];
        }

        double score = (double)sum / answers.Length;

        return score;
    }

2. Classes and objects

Class – it is like schema or template

Object – indivdual of chosen class (like presentation in PowerPoint acc. to chosen PowerPoint template;))

3. How to define class:

namespace ConsoleApp2
{
class Student
{

//here are defined fields

//…

//getters and setters

//here are defined methods

}

}

3. fields and setters and getters

a) old fashioned way – public field

public string StudentNumber; //field without getter and setter...in object you can set it like that: obj.StudentNumber="1";
b) simplified getters and setters
    public string StudentNumber2 { get; set; } //getters and setters generated in such a simple way!!! ... in bject used in the same way as above

c) getters and setters in oldfashioned way – like in Java

private int _test;

    public int getTest()
    {
        return _test;
    }

    public void setTest(int value)
    {
        _test = value;
    }

d) getters and setters with conditions (great!) .It returns boolean value on the basis of float value

public bool HasScholarship
{
get
{
if (Grade>4.5)
{
return true;
}
return false; 
} 
}

Another example:

      private double _grade;
        //inny rodzaj setera i gettera (warunkowy)
        public double Grade
        {
            get { return _grade;   }

            set
            {
                if (value<=5.0)
                {
                    _grade = value;
                }
            }
        }

4. constructors

a) default constructor

//konstruktor domyslny
public Student()
{}

b) constructor with parameters

//konstruktor z argumentami
        public Student(string studentNumberabcd,double grade)
        {
            StudentNumber = studentNumberabcd;
            Grade = grade;

        }

        public Student(string studentNumber)
        {
            StudentNumber = studentNumber;
        }

5. references – how to compare 2 objects and why „==” is not good;)

Student normalStudent = new Student("123456",3.4);
Student normalStudent2 = new Student("123456", 3.4);
//te obiekty nie sa sobie rowne - sprawdzane jest miejsce w pamieci;)
Console.WriteLine(normalStudent == normalStudent2);
        //kiedy to bedzie prawda
        Student normalStudent3 = normalStudent2;
        Console.WriteLine(normalStudent3 == normalStudent2);


        //jak wlasciwie powinno sie porownywac dwa obiekty?
        normalStudent2.Equals();

Opublikowany w C#Tagged

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *