A first look
Meet objects, types, properties, methods, static members, structs, collections and null with a small program that prints what it finds.
What you will learn#
- how to start using .NET from Ring,
- what a type, an object, a property, a method and a static member look like,
- how a struct and a collection look.
Before you start#
You need Ring and the RingDotNet package. Open a terminal in samples/UsingDotNet/tutorials. If something does not work, read Getting started. If Ring is new to you, read the Quick introduction to Ring in the appendix first.
The program#
Read the program first; the explanations follow. The line using { ... } brings classes in. func main is where Ring starts.
# a first look at .NET from Ring - runtime, types, objects, namespaces
load "dotnet.ring"
using {
"System"
"System.Text"
"System.Collections.Generic"
}
func main
# 1. The runtime that runs inside your Ring program
? "Runtime : .NET " + new Environment().Version.ToString()
# 2. Everything in .NET is an object of some type (a class)
oText = new StringBuilder("Hello")
oType = oText.GetType()
? "Type of the object: " + oType.FullName
? "Namespace : " + oType.Namespace
? "Is it a class? : " + oType.IsClass
# 3. Objects have properties (what they know) and methods (what they can do)
oText.Append(", .NET")
? "Property Length : " + oText.Length
? "Method ToString(): " + oText.ToString()
# 4. Some classes are used without creating an object: their members are static
? "Static Math.Max : " + new Math().Max(3,9)
? "Static Math.PI : " + new Math().PI
# 5. Structs are small values (a date, a point)
oDate = new DateTime(2024,12,25)
? "A DateTime : " + oDate.Format("yyyy-MM-dd") + " is a " + oDate.DayOfWeek
# 6. Collections hold many objects
oNames = new StringList()
oNames.Add("Ann")
oNames.Add("Bob")
? "A list of names : " + oNames.Count + " items, first is " + oNames.Item(0)
# 7. null means "no object". The base type of StringBuilder is Object, and Object has no base type: the answer is null
oMissing = oText.GetType().BaseType.BaseType
? "Is it null? : " + oMissing.isNull()Run it:
ring ch01_first_look.ringRuntime : .NET 9.0.15
Type of the object: System.Text.StringBuilder
Namespace : System.Text
Is it a class? : 1
Property Length : 11
Method ToString(): Hello, .NET
Static Math.Max : 9
Static Math.PI : 3.14
A DateTime : 2024-12-25 is a Wednesday
A list of names : 2 items, first is Ann
Is it null? : 1How it works, step by step#
1. The runtime#
new Environment().Version asks the .NET runtime that runs inside your Ring program for its version (Environment is a class of the System namespace that the using block opened). Nothing is installed "next to" Ring: the runtime is part of your program while it runs.
2. Everything is an object of some type#
new StringBuilder("Hello") creates an object. GetType() is a method that every .NET object has; it returns an object that describes the type. Its properties FullName, Namespace and IsClass tell you what you are holding: a class called System.Text.StringBuilder in the namespace System.Text.
When you pass a value, write the parentheses: new StringBuilder("Hello"). In Ring, init() runs only with them. Without a value both new StringBuilder and new MemoryStream() are fine, but new StringBuilder() with empty parentheses is an error; see Core concepts.
3. Properties and methods#
oText.Append(", .NET") is a method: the object does something (it adds text to itself). oText.Length is a property: it tells you something the object knows. Properties are read without parentheses.
ToString() turns the object into a Ring text. Almost every .NET object has it.
4. Static members#
new Math().Max(3,9) does not need a math object: Max belongs to the class. Ring still writes new Math() to reach it; the object is just a doorway to the class.
Ring prints the number 3.14159... as 3.14 because Ring shows two decimals by default. The value itself is not rounded. Use decimals(5) at the top of a program to see more.
5. Structs#
A DateTime is a struct: a small value. new DateTime(2024,12,25) is a date. Format("yyyy-MM-dd") formats it, and DayOfWeek is an enum, so Ring gets the text Wednesday.
ToString() has no parameters; Format("yyyy-MM-dd") is its form with a pattern. A .NET method that exists in several forms becomes one Ring method that takes one value, NULL or a list of :name = value pairs; Core concepts explains it.
6. Collections#
StringList holds texts, in order. Add adds one, Count says how many, Item(0) reads the first (the position starts at 0, like in .NET).
7. null#
null is "no object". A method that finds nothing returns it: the type Object has no base type, so oText.GetType().BaseType.BaseType (the base type of the base type of a StringBuilder) is null. isNull() checks it. Ring prints 1 for true.
What you learned#
| Idea | Example |
|---|---|
| a runtime inside your program | new Environment().Version |
| create an object | new StringBuilder("Hello") |
| a property | oText.Length |
| a method | oText.Append(", .NET") |
| a static member | new Math().Max(3,9) |
| a struct and an enum | DateTime, DayOfWeek |
| a collection | StringList |
| no object | isNull() |
Exercises#
- Change the program to print the type of a
DateTimeobject. Which namespace is it in? - Create a
StringBuilderand append your name five times with a loop. What is itsLength? - Use
new Math()to print the square root of 2 and the larger of7and19. - Find
Environment.MachineNamein the class reference (namespaceSystem) and print it.