about
CSharpBits
9/09/2022
CodeBits Repo Docs

C# Bits

organized bits of syntax, idoms, and patterns

Quick Status Recent page additions getting started testing UI format for code comparison will replace Idioms and Patterns UI layout now stable, needs more planned content
Bits are small compilable pieces of code that illustrate syntax, idioms, and small patterns. They support learning a new language by comparing bits with those of a language you already know. For now, that is limited to the programming languages C++, Rust, and C#. You can compare These C# bits with Rust or C++ bits using the navigation buttons below for each bit. If you have a wide screen or dual monitors you can right click on either of the show buttons and select "Open Link in New Window" to compare the Bits for two languages side-by-side.
C# code bits
What is this?Code demoCommentary
Hello World
The traditional first look at a new language
namespace HelloWorld { using System; public class Program { public static void Main(string[] args) { Console.WriteLine("Hello World!"); } } } C# functions are all methods of a class. They may begin with an access qualifier, a return type, accept arguments in a parentisized list, and encapsulate an executable code body in a set of statements enclosed in braces. public class UserType { public ReturnType g(ArgType a) { ... } ... } Each executable C# program starts in a static "main" function, which often calls other functions supplying operational details. There can be only one main in an executable. class MyProgram { static void main() { ... } }
Hello Objects
Demonstrates how Types are defined and instances created. The type DemoObj is defined above its main function and instances dob and cln are created in main.
// CSharpObject::Program.cs using System; namespace CSharpObject { public class DemoObject { public DemoObject(String name) { name_ = name; } public String name() { return name_; } public DemoObject clone() { DemoObject cln = new DemoObject(name_); return cln; } static void about() { Console.Write( "\n Demo creation and use of objects" ); } private String name_; } public class Program { public static void Main(string[] args) { Console.WriteLine( "\n -- demo C# object creation --" ); DemoObject dob = new DemoObject("Ashok"); Console.Write( "\n DemoObject created with name {0}", dob.name() ); DemoObject cln = dob.clone(); Console.Write( "\n clone of DemoObject has name {0}", cln.name() ); DemoObject.about(); Console.Write("\n\n That's all Folks!\n\n"); } } } Types are defined with classes, like DemoObj on the left. Functionality is provided through methods, e.g., functions associated with the class. All methods are declared and defined in the body of a class declaration.
The function DemoObj::DemoObj(String name) creates an instance of DemoObj and endows it with a name. Function DemoObj::name() implicitly accepts a reference to its instance and returns its name.
Static functions do not take a reference to self and so cannot access member data. They act like ordinary functions but are accessed with their type followed by colons followed by the function. Non-static functions take an implicit reference to self, can access member data, and are accessed from an object name followed by a period.
Types and functions declare public accessibility with the public keyword. Functions declare return types with a type name prefix.
The code at the left declares the type and operations for DemoObj. C:\..\CreateObject\CSharpObject> dotnet run /verbosity:quiet -- demonstrate C# object creation -- DemoObject created with name Ashok clone of DemoObject has name Ashok Demo creation and use of objects That's all Folks!
Hello Data
Iteration
Data Structures
Functions
Generic functions
Structs
Generic structs
Basic DIP
Generic DIP
 
  Next Prev Pages Sections About Keys