Skip to content

Instantly share code, notes, and snippets.

@snowman
Last active April 2, 2020 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snowman/166a52aa4a012b544bb0825f91e6b1e2 to your computer and use it in GitHub Desktop.
Save snowman/166a52aa4a012b544bb0825f91e6b1e2 to your computer and use it in GitHub Desktop.
C sharp basic #newbie
// chars.cs
// A listing to print out a number of characters and their numbers
//----------------------------------------------------------------
using System;
class chars
{
public static void Main()
{
int ctr;
char ch;
Console.WriteLine("\nNumber Value\n");
for( ctr = 60; ctr <= 95; ctr = ctr + 1)
{
ch = (char) ctr;
Console.WriteLine( " {0}{1}", ctr, ch.ToString().PadLeft(8, ' '));
}
}
}
// circle.cs - Using variables and literals
// This program calculates some circle stuff.
// -----------------------------------------------
using System;
class variables
{
public static void Main()
{
// Declare variables
int radius = 4;
const double PI = 3.1459;
double circum, area;
// Do calculations
area = PI * radius * radius;
circum = 2 * PI * radius;
// Print the results
Console.WriteLine("Radius = {0}, PI = {1}", radius, PI );
Console.WriteLine("The area is {0}", area);
Console.WriteLine("The circumfrance is {0}", circum);
}
}
// Score.cs Using the goto and label statements.
// Disclaimer: This program shows the use of goto and label
// This is not a good use; however, it illustrates
// the functionality of these keywords.
//--------------------------------------------------------------------
class score
{
public static void Main()
{
int score = 0;
int ctr = 0;
System.Random rnd = new System.Random();
Start:
ctr++;
if (ctr > 10)
goto EndThis;
else
score = (int) rnd.Next(60,101);
System.Console.WriteLine("{0} - You received a score of {1}",
ctr, score);
goto Start;
EndThis:
System.Console.WriteLine("Done with scores!");
}
}
// line2.cs- A class with two data members
//--------------------------------------------------------------------
class Line
{
public class Point
{
public int x;
public int y;
}
public Point starting = new Point();
public Point ending = new Point();
}
class lineApp
{
public static void Main()
{
Line line = new Line();
line.starting.x = 1;
line.starting.y = 4;
line.ending.x = 10;
line.ending.y = 11;
System.Console.WriteLine("Point 1: ({0},{1})",
line.starting.x, line.starting.y);
System.Console.WriteLine("Point 2: ({0},{1})",
line.ending.x, line.ending.y);
}
}
// ListIT.cs - program to print a listing with line numbers
//-----------------------------------------------------------
using System;
using System.IO;
class ListIT
{
public static void Main(string[] args)
{
try
{
int ctr=0;
if (args.Length <= 0 )
{
Console.WriteLine("Format: ListIT filename");
return;
}
else
{
FileStream f = new FileStream(args[0], FileMode.Open);
try
{
StreamReader t = new StreamReader(f);
string line;
while ((line = t.ReadLine()) != null)
{
ctr++;
Console.WriteLine("{0}: {1}", ctr, line);
}
f.Close();
}
finally
{
f.Close();
}
}
}
catch (System.IO.FileNotFoundException)
{
Console.WriteLine ("ListIT could not find the file {0}", args[0]);
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}\n\n", e);
}
}
}
// ex0602.cs
//-------------------------------------------
using System;
class Circle
{
int center_x;
int center_y;
double _radius;
public int x
{
get { return center_x; }
set { center_x = value; }
}
public int y
{
get { return center_y; }
set { center_y = value; }
}
public double radius
{
get { return _radius; }
set { _radius = value; }
}
}
class MyApp
{
public static void Main()
{
Circle myCircle = new Circle();
myCircle.x = 10;
myCircle.y = 10;
myCircle.radius = 8;
Console.WriteLine("Center: ({0},{1})", myCircle.x, myCircle.y);
Console.WriteLine("_radius: {0}", myCircle.radius);
Console.WriteLine("Circumference: {0}", (double) (2 * 3.14159 * myCircle.radius));
}
}
// sizes.cs -- Program to tell the size of the C# variable types
//----------------------------------------------------------------
// compile with "csc /unsafe sizes.cs"
using System;
class sizes
{
unsafe public static void Main()
{
Console.WriteLine("byte {0} byte(s)", sizeof( byte ));
Console.WriteLine("sbyte {0} byte(s)", sizeof( sbyte ));
Console.WriteLine("char {0} byte(s)", sizeof( char ));
Console.WriteLine("short {0} byte(s)", sizeof( short ));
Console.WriteLine("ushort {0} byte(s)", sizeof( ushort ));
Console.WriteLine("int {0} byte(s)", sizeof( int ));
Console.WriteLine("uint {0} byte(s)", sizeof( uint ));
Console.WriteLine("long {0} byte(s)", sizeof( long ));
Console.WriteLine("ulong {0} byte(s)", sizeof( ulong ));
Console.WriteLine("float {0} byte(s)", sizeof( float ));
Console.WriteLine("double {0} byte(s)", sizeof( double ));
Console.WriteLine("decimal {0} byte(s)", sizeof( decimal ));
Console.WriteLine("boolean {0} byte(s)", sizeof( bool ));
}
}
// xmlapp.cs - A sample C# application using XML
// documentation
//
// To get the xmlifle, run "csc /doc:xmlfile xmlapp.cs"
//-----------------------------------------------
/// <summary>
/// This is a summary describing the class.
/// </summary>
/// <remarks>
/// This is a longer comment that can be used to describe
/// the class.
/// </remarks>
class myApp
{
/// <summary>
/// The entry point for the application.
/// </summary>
/// <param name="args">A list of command line arguments</param>
public static void Main(string[] args)
{
System.Console.WriteLine("An XML Documented Program");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment