Intro to C#

Main Content

C#

C# is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework. You can use C# to create Windows client applications, XML Web services, distributed components, client-server applications, database applications, and much, much more. Visual C# provides an advanced code editor, convenient user interface designers, integrated debugger, and many other tools to make it easier to develop applications based on the C# language and the .NET Framework.



//Inner Class public class TextFieldHandler implements ActionListener { public void actionPerformed(ActionEvent event) { if (event.getSource() == display || event.getSource() == m12) { System.out.println("Action: "+ event.getActionCommand()); displayText(); } else if (event.getSource() == export || event.getSource() == m13) { System.out.println("Action: "+ event.getActionCommand()); exportToFile(); } else if (event.getSource() == m11) { System.out.println("Action: "+ event.getActionCommand()); selectInputFile(); } else if (event.getSource() == exit || event.getSource() == m14) { System.out.println("Action: "+ event.getActionCommand()); System.exit(0); } } }

public void selectInputFile()
{
// https://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html
//Get the name of the file selected
JFileChooser chooser = new JFileChooser();
//Limit the names of files to the following types
FileNameExtensionFilter filter = new FileNameExtensionFilter( "Text Files", "csv", "txt");
chooser.setFileFilter(filter);
//Show the file selection dialog
int returnVal = chooser.showOpenDialog(null);
//print the name of the file that was selected
if(returnVal == JFileChooser.APPROVE_OPTION)br {
fileName = chooser.getSelectedFile().getAbsolutePath();
//fileName = chooser.getSelectedFile().getName();
System.out.println("You chose to open this file: " + fileName);
readInputFile(fileName);
}
}

public void readInputFile(String fileName)
{ File inputFile = new File(fileName);
Scanner fileScanner = null;
try {
fileScanner = new Scanner(inputFile);
}
catch (FileNotFoundException e)
{
System.out.println("file not found " + fileName); e.printStackTrace(); }
//We now have a fileScanner object
if (fileScanner != null)
{
//Throw away the first line
fileScanner.nextLine();
studentList = new ArrayList-Student-();
while(fileScanner.hasNextLine())
{ String line = fileScanner.nextLine();
//System.out.println(line);
String [] splitLine = line.split(",");
Student aStudent = new Student();
aStudent.setFirstName(splitLine[0].trim());
aStudent.setLastName(splitLine[1].trim());
aStudent.setState(splitLine[2].trim());
aStudent.setCredits(Integer.parseInt(splitLine[3].trim()));
aStudent.setLateFee(splitLine[4].trim());
aStudent.calculateTuition();
studentList.add(aStudent);
}
if (studentList != null)
{ for (int i = 0; i is less than studentList.size(); i++)
displayString += studentList.get(i).getFirstName() + " " + studentList.get(i).getLastName() + " 1" + studentList.get(i).getTotalTuition() + "n"; totalTuition += studentList.get(i).getTotalTuition();
displayString += "Total tuition collected = $" + totalTuition; } } }
public void exportToFile()
{ // For sample code for file Save selector see: // https://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html
// In the sample, replace the following line:
// int returnVal = chooser.showOpenDialog(null);
// with // int showDialog = chooser.showSaveDialog(null);


JLabel statusMsg = new JLabel("no file selected");
JFileChooser chooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
// invoke the showsSaveDialog function to show the save dialog int showDialog = chooser.showSaveDialog(null);
// if the user selects a file
if (showDialog == JFileChooser.APPROVE_OPTION) { String outputName = chooser.getSelectedFile().getAbsolutePath(); // set the label to the path of the selected file statusMsg.setText(outputName);
try { if (displayString != null )
{ PrintWriter output = new PrintWriter(outputName);
output.print(displayString); output.close();
} else { statusMsg.setText("No data to write");
}
}
catch(Exception e) { e.getStackTrace(); } }
// if the user cancelled the operation else
{ statusMsg.setText("the user cancelled the operation"); } }
public void displayText()
{ if (displayString != null ){ textArea.setText(displayString); } } }  

***********************************************************************************************************************************************************

import javax.swing.JFrame; public class CollegeTuitionGUIMain { public static void main(String[] args) { StudentRosterGUI studentRoster = new StudentRosterGUI(); JFrame myFrame = studentRoster.getFrame(); myFrame.setVisible(true); } }

As an object-oriented language, C# supports the concepts of encapsulation, inheritance, and polymorphism. All variables and methods, including the Main method, the application's entry point, are encapsulated within class definitions. A class may inherit directly from one parent class, but it may implement any number of interfaces. Methods that override virtual methods in a parent class require the override keyword as a way to avoid accidental redefinition. In C#, a struct is like a lightweight class; it is a stack-allocated type that can implement interfaces but does not support inheritance.

namespace

Namespaces have the following properties: They organize large code projects. They are delimited by using the . operator. The using directive obviates the requirement to specify the name of the namespace for every class. The global namespace is the "root" namespace: global::System will always refer to the .NET Framework namespace System.

class

A class definition starts with the keyword class followed by the class name; and the class body enclosed by a pair of curly braces.

methods

A method is a group of statements that together perform a task. Every C# program has at least one class with a method named Main.

attributes

Attributes are a powerful feature in the C# programming language that can add metadata information to your assemblies. An attribute is actually an object that is associated with any of these elements: Assembly, Class, Method, Delegate, Enum, Event, Field, Interface, Property and Struct.

The .Net Framework

The .Net framework consists of an enormous library of codes used by the client languages such as C#.

Statements


A statement can consist of a single line of code that ends in a semicolon, or a series of single-line statements in a block. A statement block is enclosed in {} brackets and can contain nested blocks. 
\

Data Types

he variables in C#, are categorized into the following types − Value types Reference types Pointer types

C# environment

F5 will run your program