Object obj = new Object();
Class
Every class must have a constructor. Classes can also optionally have fields, properties and methods.
Instance
Static |
public class Widget
{
}
public static class Utility
{
} |
All Fields/Properties/Methods of a static class must also be static. |
Constructor
A constructor is defined with the same name as its class. A constructor is like a method without a name. A constructor is always public. It is primarily used for defaulting the fields in that instance of the object.
Default
Parameters
Advanced |
public class Widget
{
public Widget() {}
}
//class
public class Widget
{
//variable
public int ID;
//constructor
public Widget(int id)
{
ID = id;
}
}
OR
public class Widget
{
public int id;
public Widget(int id)
{
this.id = id;
}
}
//class public class Person { //variables public String firstName;
public String lastName;
public DateTime dateOfBirth;
//constructor 1 public Person(String firstName, String lastName) { this.firstName = firstName;
this.lastName = lastName;
}
//constructor 2 public Person(String firstName, String lastName, DateTime dateOfBirth) { this.firstName = firstName;
this.lastName = lastName;
this.dateOfBirth - dateOfBirth;
}
|
Widget w = new Widget() ;
Widget w =
new Widget(42);
This creates a new widget and sets the ID value to 42 |
Field
Fields within a class are basically primitive and non-primitive types that define it.
Instance
Static |
public class Widget
{
public int ID;
}
public class Widget
{
public static string Desc;
} |
w.ID = 42;
Console.Write(w.ID);
Widget.Desc = "Hmmm...";
Console.Write(Widget.Desc); |
Property
Properties allow setting and getting values for fields. They also restrict access by the level defined (public, private, protected and internal etc).
Shorthand
Explicit |
public class Widget
{
public int ID
{
get;
protected set;
}
}
public class Widget
{
private int id;
public int ID;
{
get
{
return id;
}
set
{
if (value == 0)
value = 1;
id = value;
}
}
} |
The following will work; because the property getter is public:
Console.Write( Widget.ID );
The following is not valid; because the property setter is not public.
Widget.ID = 1337; |
Method
Methods are functions of the class that can be run within or against the class as long as they are not static. Static can only be run against the class.
All methods take variables as arguments and can return a single variable.
A method is defined by:
{access level (member access modifier)}
{keyword or reference modifier} {return type} {name of method}
(args)
<-- each arg has a type and name, comma separated.
Static methods can only be accessed from static methods and fields.
Non-static methods can access all the static and non-static methods by going through the class.
Without parameters
With parameters
Void method
Return value |
public class Widget
{
public int ID;
public void GenerateID()
{
Random randy.Next(0, int.MaxValue);
}
}
public class Widget
{
public int ID;
public void GenerateID(int min, int max)
{
Random randy = new Random();
ID = randy.Next(min, max)
}
}
The above two examples are void methods, because they do not return a value; they just perform some action(s) internally.
public class Widget
{
public int ID;
public bool isValid()
{
return (ID % 2 == 0);
}
}
|
Widget w = new Widget();
w.GenerateID();
This lets you specify the range of possible random ID values:
Widget w = new Widget();
w.GenerateID(0, 255);
Widget w = new Widget();
w.ID = 13;
if (w.IsValid())
{
Console.Write("valid widget!");
}
else
{
System.Exit();
}
|
Constants
Constants allow setting a field to read only and can only be set with a literal.
Constants cannot be changed once defined.
public const in OS_VERSION = 1
Good coding style is to capitalise all letters within the constant and to also separate words with _ (Underscore).
Override
To override a methods class within a subclass you will need to add virtual to the main default class so that override can be added to the sub-class.
in Visual Studio you can type override<tab x 2>
and it will pre-fill the method details.
eg.
public override string ToString()
{
return "Hello";
}
Bracket use in C#
- { } = delimit/define the context of variables and fields indicating the beginning and end of a code block and sometimes for the definition of literal data values such as structures and arrays.
- < > = delimit generic arguments.
- ( ) = define operator precedence and to contain the parameters and arguments of a function.
- [ ] = define literal arrays or lists or to denote metadata attributes.