public
// Note that namespaces can have
// subdirectories, as Models here
namespace MyAwesomeApp.Models
{
public class Student
{
}
}
// This is how you would use the
// Student class in other code
using MyAwesomeApp.Models
Program
when creating a new console application:class Program
{
static void Main(string[] args)
{
}
}
class Student
{
}
Program
class:class Student
{
int id;
string name;
}
new
keywordStudent
are created
namespace MyAwesomeApp
{
class Student
{
}
class Program
{
static void Main(string[] args)
{
Student student1 = new Student();
Student student2 = new Student();
Student student3 = new Student();
}
}
}
.
operatorclass Student
{
int id;
}
Error:
Program class:
class Program
{
static void Main(string[] args)
{
Student student = new Student();
student.id = 12345678;
}
}
Let's fix the previous example by changing the access specifier of the variable id
to public
:
class Student
{
public int id;
}
class Program
{
static void Main(string[] args)
{
Student student = new Student();
student.id = 12345678;
}
}
id
of the object student
is now 12345678
private
are accessible only inside the containing class
public
are accessible outside of the classclass Student
{
int id; // Accessible only inside the class
private string name; // Accessible only inside the class
public string address; // Accessible everywhere within the namespace
}
Continuing on the class in the previous example: if we follow the student
variable with a dot, Visual Studio IntelliSense will only suggest the address
variable, because it was the only public
variable of the Student
class!
class Student
{
public int id;
public string name;
}
class Program
{
static void Main(string[] args)
{
Student student1 = new Student();
Student student2 = new Student();
student1.id = 22225555;
student1.name = "Johannes Kantola";
student2.id = 44441111;
student2.name = "Rene Orosz";
Console.WriteLine(student1.name);
// Outputs Johannes Kantola
}
}
User
that contains two fields: string userName
and string password
User
instance and that instance is saved to a list of usersclass Person
{
public string firstName;
public string lastName;
public string FullName()
{
return firstName + " " + lastName;
}
}
class Program
{
static void Main(string[] args)
{
Person someDude = new Person();
someDude.firstName = "Johannes";
someDude.lastName = "Kantola";
Console.WriteLine(someDude.FullName());
// Outputs Johannes Kantola
}
}
class User
{
public User()
{
Console.WriteLine
("New user created!");
}
}
class Program
{
static void Main(string[] args)
{
User user = new User();
// Outputs "New user created!"
}
}
ctor
and press tab twice to quickly create a constructor!class Car
{
private string model;
private int year;
private int doors;
public Car(string modelName, int modelYear)
{
model = modelName;
year = modelYear;
doors = 5;
}
}
class Program
{
static void Main(string[] args)
{
Car lada = new Car("Niva", 1984);
Car ford = new Car("Focus", 2010);
// Both cars have 5 doors by default
}
}
name
and sound
Greet()
that prints <name> says <sound>!
(without the angled brackets)get
and set
accessors
class User
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
}
class Program
{
static void Main(string[] args)
{
User admin = new User();
admin.Id = 12345678;
Console.WriteLine(admin.Id);
// This outputs 12345678
}
}
class User
{
public int Id { get; set; }
}
class Program
{
static void Main(string[] args)
{
User admin = new User();
admin.Id = 12345678;
Console.WriteLine(admin.Id);
// This outputs 12345678
}
}
prop
and press tab twice to quickly create an auto propertypublic int Id { get; } // This field is read-only
public int Password { private get; set; } // This field is write-only
int
, double
, char
and other primitive data types are of the value type
class Program{
static void Main(string[] args)
{
int user = 1;
int otherUser = user;
user = 2;
Console.WriteLine("Value of otherUser: " + otherUser);
}
}
string
, Array
and Classes are of the *reference type
class Program
{
static void Main(string[] args)
{
User user = new User();
user.id = 1;
User otherUser = user;
user.id = 2;
Console.WriteLine(
"Value of otherUser.id: " + otherUser.id);
}
}
class User
{
public int id;
}
TestClass
.TestClass
. The compiler should not give any errors.TestClass
is in to TestNamespace
. Go back to your main method. The TestClass
instantiation should not work anymore. Try to fix the issue without going back to TestClass
!Location in memory int userId = 42; int otherUserId = userId; | 42 | | :-: | | 42 | Changing the value in the object will affect all variables referencing it! | points to | | :-: | | points to | User user = new User(42); User otherUser = user; | user { id = 42 } | | :-: |
## Exercise 4 Modify previous solution by using methods and class.