public Null() { // Null is the value of nothing // Classes and strings can always be null CarExample car = null; // <-- No problem string myString = null; // <-- No problem // Integers cannot be null //int myInteger = null; // <-- Will not compile! // We need to indicate that it is nullable int?myNullableInteger = null; // <-- This works // What's going on here? // Reference types can always be null // Value types can never be null unless they are declared with a question mark after their type // Let's talk about strings // Strings are special // They are value types, but they always act as if they were declared with a question mark after their type // Basically, you can think of string as always being string? implicitly }
// Declarations in C# do not default to public // Explicitly declaring access modifiers is never a bad idea public LearningClasses() { // Let's create a "Car" class // Static declarations can be used through the class directly // Set makes of cars CarExample.Makes = new List <string> { "Ford", "Honda", "GMC", "Toyota", "Hyundai" }; // Member declarations can be used only by an instanced of the class // First, we must instantiate a car var car = new CarExample(); // Then, we can set its color car.Color = "Red"; // But, wait, there's a better way to instantiate! // We can create an instance and set member properties all at once var car2 = new CarExample() { Color = "Silver" }; // When we instantiate a class we do not necessarily need to use parenthesises and curly brackets // We don't have to use both var car3 = new CarExample // <-- Look ma, no parenthesises! { Color = "Black" }; // When we use curly brackets, we are never required to set properties var car4 = new CarExample { }; // <-- This is valid }
//-------------------------------------------------------------------- // Build the Insert command String //-------------------------------------------------------------------- private String BuildInsertCommand(CarExample car) { String command; StringBuilder sb = new StringBuilder(); // use a string builder to create the dynamic string sb.AppendFormat("Values('{0}', '{1}' ,{2}, {3})", car.Model, car.Manufacturer, car.Year.ToString(), car.Price.ToString()); String prefix = "INSERT INTO Cars_2020 " + "(model, manufacturer, year, price) "; command = prefix + sb.ToString(); return(command); }
public static void ExampleMain() { // This byte array is used for encoding and decoding, this is what you would send on the wire or save to disk var byteBuffer = new byte[4096]; // You need to "wrap" the array with a DirectBuffer, this class is used by the generated code to read and write efficiently to the underlying byte array var directBuffer = new DirectBuffer(byteBuffer); const short SchemaVersion = 0; int bufferOffset = 0; var MessageHeader = new MessageHeader(); var Car = new Car(); // Before encoding a message we need to create a SBE header which specify what we are going to encode (this will allow the decoder to detect that it's an encoded 'car' object) // We will probably simplify this part soon, so the header gets applied automatically, but for now it's manual MessageHeader.Wrap(directBuffer, bufferOffset, Car.SchemaVersion); // position the MessageHeader on the DirectBuffer, at the correct position MessageHeader.BlockLength = Car.BlockLength; // size that a car takes on the wire MessageHeader.SchemaId = Car.SchemaId; MessageHeader.TemplateId = Car.TemplateId; // identifier for the car object (SBE template ID) MessageHeader.Version = Car.SchemaVersion; // this can be overriden if we want to support different versions of the car object (advanced functionality) // Now that we have encoded the header in the byte array we can encode the car object itself bufferOffset += MessageHeader.Size; CarExample.Encode(Car, directBuffer, bufferOffset); // Now we have encoded the message is the byte array, we are going to decode it // first we decode the header (in a real world scenario you would need the header to decide which SBE decoder you are going to use bufferOffset = 0; // position the MessageHeader object at the beginning of the array MessageHeader.Wrap(directBuffer, bufferOffset, SchemaVersion); // Extract info from the header // In a real app you would use that to lookup the applicable flyweight to decode this type of message based on templateId and version. int actingBlockLength = MessageHeader.BlockLength; int actingVersion = MessageHeader.Version; bufferOffset += MessageHeader.Size; // now we decode the message CarExample.Decode(Car, directBuffer, bufferOffset, actingBlockLength, actingVersion); }
//-------------------------------------------------------------------------------------------------- // This method inserts a car to the cars table //-------------------------------------------------------------------------------------------------- public int insert(CarExample car) { SqlConnection con; SqlCommand cmd; try { con = connect("carsDBConnectionString"); // create the connection } catch (Exception ex) { // write to log throw (ex); } String cStr = BuildInsertCommand(car); // helper method to build the insert string cmd = CreateCommand(cStr, con); // create the command try { int numEffected = cmd.ExecuteNonQuery(); // execute the command return(numEffected); } catch (Exception ex) { return(0); // write to log throw (ex); } finally { if (con != null) { // close the db connection con.Close(); } } }
static void Main(string[] args) { TcpListener server = null; try { // Set the TcpListener on port 13000. Int32 port = 13000; IPAddress localAddr = IPAddress.Parse("127.0.0.1"); // TcpListener server = new TcpListener(port); server = new TcpListener(localAddr, port); // Start listening for client requests. server.Start(); // Buffer for reading data Byte[] bytes = new Byte[256]; // Enter the listening loop. while (true) { Console.Write("Waiting for a connection... "); // Perform a blocking call to accept requests. // You could also user server.AcceptSocket() here. TcpClient client = server.AcceptTcpClient(); Console.WriteLine("Connected!"); // Get a stream object for reading and writing NetworkStream stream = client.GetStream(); int i; // Loop to receive all the data sent by the client. while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { var byteBuffer = new byte[4096]; // You need to "wrap" the array with a DirectBuffer, this class is used by the generated code to read and write efficiently to the underlying byte array var directBuffer = new DirectBuffer(bytes); int bufferOffset = 0; const short SchemaVersion = 0; string type = "qatsbeengine"; switch (type) { #region car case "car": var MessageHeader = new Sbe.MessageHeader(); var Car = new Sbe.Car(); // position the MessageHeader object at the beginning of the array MessageHeader.Wrap(directBuffer, bufferOffset, SchemaVersion); Console.WriteLine("MessageHeader.BlockLength=" + MessageHeader.BlockLength); Console.WriteLine("MessageHeader.TemplateId=" + MessageHeader.TemplateId); Console.WriteLine("MessageHeader.SchemaId=" + MessageHeader.SchemaId); Console.WriteLine("MessageHeader.Version=" + MessageHeader.Version); // Extract info from the header // In a real app you would use that to lookup the applicable flyweight to decode this type of message based on templateId and version. int actingBlockLength = MessageHeader.BlockLength; int actingVersion = MessageHeader.Version; bufferOffset += Sbe.MessageHeader.Size; // now we decode the message CarExample.Decode(Car, directBuffer, bufferOffset, actingBlockLength, actingVersion); break; #endregion case "fix": var MessageHeaderFix = new SbeFIX.MessageHeader(); var sno = new NegotiateResponse(); // position the MessageHeader object at the beginning of the array MessageHeaderFix.Wrap(directBuffer, bufferOffset, SchemaVersion); Console.WriteLine("MessageHeader.BlockLength=" + MessageHeaderFix.BlockLength); Console.WriteLine("MessageHeader.TemplateId=" + MessageHeaderFix.TemplateId); Console.WriteLine("MessageHeader.SchemaId=" + MessageHeaderFix.SchemaId); Console.WriteLine("MessageHeader.Version=" + MessageHeaderFix.Version); // Extract info from the header // In a real app you would use that to lookup the applicable flyweight to decode this type of message based on templateId and version. int actingBlockLengthFix = MessageHeaderFix.BlockLength; int actingVersionFix = MessageHeaderFix.Version; bufferOffset += SbeFIX.MessageHeader.Size; // now we decode the message NewOrderSingleExample.Decode(sno, directBuffer, bufferOffset, actingBlockLengthFix, actingVersionFix); break; case "qatsbeengine": try { SbeReflectionWrapper _Wrapper = new SBEReflection.SbeReflectionWrapper(); SbeLoader.Load(@"C:\Users\Akio\source\repos\POC_SBE\packages\sbe-tool.1.17.0\tools\fixp-entrypoint-messages-1.2.xml"); Console.WriteLine(_Wrapper.DecodeSBEMessageQATEngine(bytes)); } catch (Exception ex) { Console.WriteLine(ex.Message); } break; case "reflection": try { SbeReflectionWrapper _Wrapper = new SBEReflection.SbeReflectionWrapper(@"C:\Users\Akio\source\repos\POC_SBE\fixp-entrypoint-messages-1.2\bin\Debug\fixp-entrypoint-messages-1.2.dll"); SbeLoader.Load(@"C:\Users\Akio\source\repos\POC_SBE\packages\sbe-tool.1.17.0\tools\fixp-entrypoint-messages-1.2.xml"); Console.WriteLine(_Wrapper.DecodeSBEMessage(bytes)); } catch (Exception ex) { Console.WriteLine(ex.Message); } break; } } // Shutdown and end connection client.Close(); } } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } finally { // Stop listening for new clients. server.Stop(); } Console.WriteLine("\nHit enter to continue..."); Console.Read(); }