C Sharp: Difference between revisions
imported>Andrew Cuthbert (Basic stub) |
mNo edit summary |
||
(44 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
{{subpages}} | |||
{{TOC|right}} | |||
{{dambigbox|text=For other uses, see [[C (disambiguation)]].}} | |||
'''C#''' is a main-stream, general-purpose [[programming language]] developed at [[Microsoft]]. C# is fully [[object-oriented programming|object-oriented]] and is under continuous active development by Microsoft. C# programs are fully portable across all recent Microsoft operating systems since [[Windows 2000]]/[[Windows XP|XP]] (and partially on [[Windows 98]]). The latest version of C# (and the virtual machine software it runs on, [[.NET Framework]] v6.0) as of April 2022 is 10.0. These were released in 2021. | |||
To execute on Windows, C# programs require the installation of the [[.NET Framework]] [[Common Runtime]] (CLR), a [[virtual machine]] that hides [[operating system]] details. The ''runtime'', as it is sometimes called, is available both for [[Windows]] [[client computer|client computers]] and Microsoft [[Web server|Web servers]]. C# programs consist of .exe or .dll files, each containing [[Common Intermediate Language]] (CIL) instructions which will execute in the CLR, a manifest (table of contents), [[metadata]] pertaining to the program, and (possibly) encapsulated resources such as images or audio. | |||
C# is deliberately similar to the [[Java programming language]] and [[Java platform]]<ref>Mark Johnson, [http://www.javaworld.com/jw-11-2000/jw-1122-csharp1.html C#: A language alternative or just J--?], JavaWorld.com</ref>. | |||
In C# 3.0, Microsoft added the 'var' keyword, which allows type inference<ref>Sahil Malik, [http://www.developer.com/net/csharp/article.php/3561756 A Preview of What's New in C# 3.0], developer.com</ref>. For instance, one might declare: | |||
<code>var fooString = "Hello, World!";</code> | |||
when in previous versions, one might have to write: | |||
<code>string fooString = "Hello, World!";</code> | |||
== Popularity == | |||
C# has remained popular for over twenty years, especially for Windows server applications due to its high performance, reliability, and retaining of backwards compatibility for older programs. As of April 2022, the PYPL index ranks it 4th, and the TIOBE index 5th, in popularity, with an overall user share estimated at 7.5%<ref>[https://statisticstimes.com/tech/top-computer-languages.php Top Computer Languages] from TIOBE and PYPL indexes; last access 4/3/2022</ref>. Both indeces show its usage as slowly increasing. | |||
== Standards and Implementations== | |||
C# is standardized by ECMA (the ECMA-334 standard) and by ISO/IEC (the ISO/IEC 23270 standard). Microsoft’s C# for the .NET Framework is a conforming implementation of both of these standards. An independent version of the Common Language Runtime (not developed by Microsoft) is available as a result of the [[open source]] [[Mono Project]]<ref>[http://mono-project.com/ Mono Project], sponsored by [[Novell]].</ref>; it provides software to develop and run .NET applications on [[Linux]], [[Solaris]], [[Mac OS X]], [[Windows]], and [[Unix]]. | |||
== Code Example == | |||
C# syntax is in the C programming language family and is also very similar to Java (in more than just syntax). C# uses left and right brackets to contain code segments. Below is sample code for the About form of a Windows application: | |||
<syntaxhighlight lang="C#" line> | |||
using System; | |||
using System.Drawing; | |||
using System.Collections; | |||
using System.ComponentModel; | |||
using System.Windows.Forms; | |||
namespace Bubbledesk | |||
{ | |||
public class AboutForm : System.Windows.Forms.Form | |||
{ | |||
private System.Windows.Forms.Label label1; | |||
private System.Windows.Forms.PictureBox pictureBox1; | |||
private System.Windows.Forms.Button buttonClose; | |||
private System.ComponentModel.IContainer components = null; | |||
// do not delete! | |||
#region comment block | |||
/// <summary> | |||
/// Required designer variable. | |||
/// </summary> | |||
#endregion | |||
public AboutForm() | |||
{ | |||
// required for Windows Form Designer support | |||
InitializeComponent(); | |||
// add any constructor code after InitializeComponent call | |||
} | |||
protected override void Dispose( bool disposing ) | |||
{ | |||
if( disposing ) | |||
{ | |||
if(components != null) | |||
{ | |||
components.Dispose(); | |||
} | |||
} | |||
base.Dispose( disposing ); | |||
} | |||
#region Windows Form Designer generated code | |||
/// <summary> | |||
/// Required method for Designer support - do not modify | |||
/// the contents of this method with the code editor. | |||
/// </summary> | |||
private void InitializeComponent() | |||
{ | |||
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(AboutForm)); | |||
this.label1 = new System.Windows.Forms.Label(); | |||
this.pictureBox1 = new System.Windows.Forms.PictureBox(); | |||
this.buttonClose = new System.Windows.Forms.Button(); | |||
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); | |||
this.SuspendLayout(); | |||
// | |||
// label1 | |||
// | |||
this.label1.BackColor = System.Drawing.Color.Transparent; | |||
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); | |||
this.label1.ForeColor = System.Drawing.Color.Black; | |||
this.label1.Location = new System.Drawing.Point(16, 48); | |||
this.label1.Name = "label1"; | |||
this.label1.Size = new System.Drawing.Size(408, 159); | |||
this.label1.TabIndex = 0; | |||
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; | |||
// | |||
// pictureBox1 | |||
// | |||
this.pictureBox1.BackColor = System.Drawing.Color.Transparent; | |||
this.pictureBox1.Location = new System.Drawing.Point(208, 8); | |||
this.pictureBox1.Name = "pictureBox1"; | |||
this.pictureBox1.Size = new System.Drawing.Size(40, 32); | |||
this.pictureBox1.TabIndex = 2; | |||
this.pictureBox1.TabStop = false; | |||
// | |||
// buttonClose | |||
// | |||
this.buttonClose.FlatStyle = System.Windows.Forms.FlatStyle.System; | |||
this.buttonClose.Location = new System.Drawing.Point(196, 210); | |||
this.buttonClose.Name = "buttonClose"; | |||
this.buttonClose.Size = new System.Drawing.Size(64, 24); | |||
this.buttonClose.TabIndex = 3; | |||
this.buttonClose.Text = "Close"; | |||
this.buttonClose.Click += new System.EventHandler(this.buttonClose_Click); | |||
// | |||
// AboutForm | |||
// | |||
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage"))); | |||
this.ClientSize = new System.Drawing.Size(449, 246); | |||
this.Controls.Add(this.buttonClose); | |||
this.Controls.Add(this.pictureBox1); | |||
this.Controls.Add(this.label1); | |||
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; | |||
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); | |||
this.MaximizeBox = false; | |||
this.Name = "AboutForm"; | |||
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; | |||
this.Text = "AboutForm"; | |||
this.Load += new System.EventHandler(this.AboutForm_Load); | |||
this.Closing += new System.ComponentModel.CancelEventHandler(this.AboutForm_Closing); | |||
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); | |||
this.ResumeLayout(false); | |||
} | |||
#endregion | |||
private static ProgramInfo myProgram; | |||
private void AboutForm_Load(object sender, System.EventArgs e) | |||
{ | |||
string stringMe = "AboutForm_Load: "; | |||
try | |||
{ | |||
myProgram = ProgramInfo.GetInstance(); myProgram.SetFormType(this); | |||
this.CenterToParent(); | |||
this.Text = "About " + Application.ProductName + | |||
" " + myProgram.VersionShort; | |||
this.pictureBox1.Image = Owner.Icon.ToBitmap(); | |||
string tmpstr = myProgram.Description + | |||
"\n\r\n\r" + "Version " + myProgram.Version + | |||
"\n\r\n\r" + myProgram.Company + | |||
"\n\r\n\r" + myProgram.Copyright; | |||
this.label1.Text = tmpstr; | |||
} | |||
catch (Exception ex) | |||
{ throw new Exception(stringMe + ex.Message); } | |||
catch | |||
{ throw new Exception(stringMe + "unknown issue"); } | |||
} // end AboutForm_Load | |||
private void buttonClose_Click(object sender, System.EventArgs e) | |||
{ | |||
string stringMe = "AboutForm.buttonClose_Click: "; | |||
try | |||
{ | |||
this.Close(); | |||
} | |||
catch (Exception ex) | |||
{ throw new Exception(stringMe + ex.Message); } | |||
catch | |||
{ throw new Exception(stringMe + "unknown issue"); } | |||
} | |||
private void AboutForm_Closing(object sender, System.ComponentModel.CancelEventArgs e) | |||
{ | |||
string stringMe = "AboutForm_Closing: "; | |||
try | |||
{ | |||
if (myProgram!=null) myProgram = null; | |||
} | |||
catch (Exception ex) | |||
{ throw new Exception(stringMe + ex.Message); } | |||
catch | |||
{ throw new Exception(stringMe + "unknown issue"); } | |||
} | |||
} // end class | |||
} // end namespace | |||
</syntaxhighlight> | |||
==References== | |||
<references />[[Category:Suggestion Bot Tag]] |
Latest revision as of 16:00, 23 July 2024
C# is a main-stream, general-purpose programming language developed at Microsoft. C# is fully object-oriented and is under continuous active development by Microsoft. C# programs are fully portable across all recent Microsoft operating systems since Windows 2000/XP (and partially on Windows 98). The latest version of C# (and the virtual machine software it runs on, .NET Framework v6.0) as of April 2022 is 10.0. These were released in 2021.
To execute on Windows, C# programs require the installation of the .NET Framework Common Runtime (CLR), a virtual machine that hides operating system details. The runtime, as it is sometimes called, is available both for Windows client computers and Microsoft Web servers. C# programs consist of .exe or .dll files, each containing Common Intermediate Language (CIL) instructions which will execute in the CLR, a manifest (table of contents), metadata pertaining to the program, and (possibly) encapsulated resources such as images or audio.
C# is deliberately similar to the Java programming language and Java platform[1].
In C# 3.0, Microsoft added the 'var' keyword, which allows type inference[2]. For instance, one might declare:
var fooString = "Hello, World!";
when in previous versions, one might have to write:
string fooString = "Hello, World!";
Popularity
C# has remained popular for over twenty years, especially for Windows server applications due to its high performance, reliability, and retaining of backwards compatibility for older programs. As of April 2022, the PYPL index ranks it 4th, and the TIOBE index 5th, in popularity, with an overall user share estimated at 7.5%[3]. Both indeces show its usage as slowly increasing.
Standards and Implementations
C# is standardized by ECMA (the ECMA-334 standard) and by ISO/IEC (the ISO/IEC 23270 standard). Microsoft’s C# for the .NET Framework is a conforming implementation of both of these standards. An independent version of the Common Language Runtime (not developed by Microsoft) is available as a result of the open source Mono Project[4]; it provides software to develop and run .NET applications on Linux, Solaris, Mac OS X, Windows, and Unix.
Code Example
C# syntax is in the C programming language family and is also very similar to Java (in more than just syntax). C# uses left and right brackets to contain code segments. Below is sample code for the About form of a Windows application:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Bubbledesk
{
public class AboutForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button buttonClose;
private System.ComponentModel.IContainer components = null;
// do not delete!
#region comment block
/// <summary>
/// Required designer variable.
/// </summary>
#endregion
public AboutForm()
{
// required for Windows Form Designer support
InitializeComponent();
// add any constructor code after InitializeComponent call
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(AboutForm));
this.label1 = new System.Windows.Forms.Label();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.buttonClose = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.BackColor = System.Drawing.Color.Transparent;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.ForeColor = System.Drawing.Color.Black;
this.label1.Location = new System.Drawing.Point(16, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(408, 159);
this.label1.TabIndex = 0;
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.Transparent;
this.pictureBox1.Location = new System.Drawing.Point(208, 8);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(40, 32);
this.pictureBox1.TabIndex = 2;
this.pictureBox1.TabStop = false;
//
// buttonClose
//
this.buttonClose.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.buttonClose.Location = new System.Drawing.Point(196, 210);
this.buttonClose.Name = "buttonClose";
this.buttonClose.Size = new System.Drawing.Size(64, 24);
this.buttonClose.TabIndex = 3;
this.buttonClose.Text = "Close";
this.buttonClose.Click += new System.EventHandler(this.buttonClose_Click);
//
// AboutForm
//
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(449, 246);
this.Controls.Add(this.buttonClose);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "AboutForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "AboutForm";
this.Load += new System.EventHandler(this.AboutForm_Load);
this.Closing += new System.ComponentModel.CancelEventHandler(this.AboutForm_Closing);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private static ProgramInfo myProgram;
private void AboutForm_Load(object sender, System.EventArgs e)
{
string stringMe = "AboutForm_Load: ";
try
{
myProgram = ProgramInfo.GetInstance(); myProgram.SetFormType(this);
this.CenterToParent();
this.Text = "About " + Application.ProductName +
" " + myProgram.VersionShort;
this.pictureBox1.Image = Owner.Icon.ToBitmap();
string tmpstr = myProgram.Description +
"\n\r\n\r" + "Version " + myProgram.Version +
"\n\r\n\r" + myProgram.Company +
"\n\r\n\r" + myProgram.Copyright;
this.label1.Text = tmpstr;
}
catch (Exception ex)
{ throw new Exception(stringMe + ex.Message); }
catch
{ throw new Exception(stringMe + "unknown issue"); }
} // end AboutForm_Load
private void buttonClose_Click(object sender, System.EventArgs e)
{
string stringMe = "AboutForm.buttonClose_Click: ";
try
{
this.Close();
}
catch (Exception ex)
{ throw new Exception(stringMe + ex.Message); }
catch
{ throw new Exception(stringMe + "unknown issue"); }
}
private void AboutForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
string stringMe = "AboutForm_Closing: ";
try
{
if (myProgram!=null) myProgram = null;
}
catch (Exception ex)
{ throw new Exception(stringMe + ex.Message); }
catch
{ throw new Exception(stringMe + "unknown issue"); }
}
} // end class
} // end namespace
References
- ↑ Mark Johnson, C#: A language alternative or just J--?, JavaWorld.com
- ↑ Sahil Malik, A Preview of What's New in C# 3.0, developer.com
- ↑ Top Computer Languages from TIOBE and PYPL indexes; last access 4/3/2022
- ↑ Mono Project, sponsored by Novell.